{"id":302,"date":"2018-07-24T23:24:10","date_gmt":"2018-07-24T23:24:10","guid":{"rendered":"http:\/\/www.datastructure.org\/?p=302"},"modified":"2025-04-21T13:49:11","modified_gmt":"2025-04-21T13:49:11","slug":"left-and-right-views-in-a-binary-tree","status":"publish","type":"post","link":"https:\/\/www.bhargaviurs.com\/index.php\/2018\/07\/24\/left-and-right-views-in-a-binary-tree\/","title":{"rendered":"Left and Right Views in a Binary Tree"},"content":{"rendered":"<p><strong>Right View &#8211;<\/strong> All the nodes visible when viewed from the right side of a tree.<\/p>\n<p><strong>Left View &#8211;<\/strong> All the nodes visible when viewed from the left side of a tree.<\/p>\n<p>Consider a simple example below. Notice the views.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-321\" src=\"http:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.39.13-PM.png\" alt=\"\" width=\"2804\" height=\"1332\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.39.13-PM.png 2804w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.39.13-PM-300x143.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.39.13-PM-1024x486.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.39.13-PM-768x365.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.39.13-PM-1536x730.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.39.13-PM-2048x973.png 2048w\" sizes=\"auto, (max-width: 2804px) 100vw, 2804px\" \/><\/p>\n<p>Consider another example below where the root node A does not have left subtree. Notice the left and right views.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-322\" src=\"http:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.41.48-PM.png\" alt=\"\" width=\"2688\" height=\"1504\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.41.48-PM.png 2688w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.41.48-PM-300x168.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.41.48-PM-1024x573.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.41.48-PM-768x430.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.41.48-PM-1536x859.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.41.48-PM-2048x1146.png 2048w\" sizes=\"auto, (max-width: 2688px) 100vw, 2688px\" \/><\/p>\n<p>One of the method is to do a <strong>Level Order Traversal<\/strong> on the tree to get the views.<br><strong>Left View&nbsp;<\/strong>&#8211; When we do a level order traversal, notice that the <strong>first<\/strong> element in each level gives the left view of the tree.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-323\" src=\"http:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.43.01-PM.png\" alt=\"\" width=\"3008\" height=\"1696\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.43.01-PM.png 3008w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.43.01-PM-300x169.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.43.01-PM-1024x577.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.43.01-PM-768x433.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.43.01-PM-1536x866.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.43.01-PM-2048x1155.png 2048w\" sizes=\"auto, (max-width: 3008px) 100vw, 3008px\" \/><\/p>\n<p><strong>Right View<\/strong> &#8211;&nbsp;When we do a level order traversal, notice that the&nbsp;<b>last<\/b>&nbsp;element in each level gives the right view of the tree.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-326\" src=\"http:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.51.26-PM.png\" alt=\"\" width=\"2976\" height=\"1678\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.51.26-PM.png 2976w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.51.26-PM-300x169.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.51.26-PM-1024x577.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.51.26-PM-768x433.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.51.26-PM-1536x866.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/07\/Screen-Shot-2018-07-24-at-6.51.26-PM-2048x1155.png 2048w\" sizes=\"auto, (max-width: 2976px) 100vw, 2976px\" \/><\/p>\n<p><strong>Implementation<\/strong>: <strong>Recursive solution<\/strong><\/p>\n<p><strong>Logic:<\/strong><\/p>\n<p>One way to solve this problem is by doing Level Order Traversal.<\/p>\n<ol>\n<li>Keep track of each level starting with level = 1.<\/li>\n<li>Store the last node in each level. Use a list or array to store the last node in each level.<\/li>\n<\/ol>\n<p>Before we jump into coding, let&#8217;s understand little bit of the logic here. In each recursive traversal, we check if the current level is greater than the number of nodes in the right view that is encountered. If that is true, then append the last node to the list in the current level.<\/p>\n<p>let right_view_nodes = []<\/p>\n<p>When <strong>level = 1<\/strong>, right_view_nodes is initially empty <strong>[]<\/strong>, then we add the last node in this level which is A, go to the next level.<\/p>\n<p><strong>level = 2<\/strong>, right_view_nodes will have <strong>[&#8216;A&#8217;]<\/strong>, then we add the last node in this level which is C<\/p>\n<p><strong>level = 3<\/strong>, right_view_nodes will have <strong>[&#8216;A&#8217;, &#8216;C&#8217;]<\/strong>, then we add the last node in this level which is G.<\/p>\n<p><strong>level = 4<\/strong>, right_view_nodes will have <strong>[&#8216;A&#8217;, &#8216;C&#8217;, &#8216;G&#8217;]<\/strong>, then we add the last node in this level which is K.<\/p>\n<p><strong>level = 5<\/strong>, right_view_nodes will have <strong>[&#8216;A&#8217;, &#8216;C&#8217;, &#8216;G&#8217;, &#8216;K&#8217;],<\/strong> then we add the last node in this level which is N.<\/p>\n<p>Basically, as long as the current <strong>level &gt; len(right_view_nodes) or len(right_view_nodes) &lt; level<\/strong>, we continue to <strong>append the last node<\/strong> at each level to the list.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<p>&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">def treeTraversal(root, right_view_nodes, level):\n    \"\"\"\n    :param obj root: root node of the tree to be traversed.\n    :param list right_view_nodes: list of the nodes in the right view.\n    :param int level: current level of the tree.\n    :return:\n    \"\"\"\n    if root is None:\n        return\n    if len(right_view_nodes) &lt; level:\n        right_view_nodes.append(root.value)\n    level = level + 1\n    # Right View: traverse right subtree first.\n    treeTraversal(root.right, right_view_nodes, level)\n    treeTraversal(root.left, right_view_nodes, level)\n    # Left View: traverse left subtree first.\n    # treeTraversal(root.left, right_view_nodes, level)\n    # treeTraversal(root.right, right_view_nodes, level)\ndef rightView(root):\n    \"\"\"\n    :param obj root: root node of the tree to be traversed.\n    :return list: right view nodes of the tree.\n    \"\"\"\n    right_view_nodes = []\n    level = 1\n    treeTraversal(root, right_view_nodes, level)\n    return right_view_nodes\nclass TreeNode(object):\n    def __init__(self, value):\n        self.left = None\n        self.right = None\n        self.data = value\nroot = TreeNode('A')\nroot.left = TreeNode('B')\nroot.right = TreeNode('C')\nroot.left.left = TreeNode('D')\nroot.left.right = TreeNode('E')\nroot.right.left = TreeNode('F')\nroot.right.right = TreeNode('G')\nroot.left.right.left = TreeNode('I')\nroot.right.left.right = TreeNode('K')\nroot.left.right.left.left = TreeNode('L')\nroot.left.right.left.right = TreeNode('M')\nroot.right.left.right.right = TreeNode('N')\nprint rightView(root)\n# Output\nRight View: ['A', 'C', 'G', 'K', 'N']\nLeft View: ['A', 'B', 'D', 'I', 'L']<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Right View &#8211; All the nodes visible when viewed from the right side of a tree. Left View &#8211; All the nodes visible when viewed from the left side of a tree. Consider a simple example below. Notice the views. Consider another example below where the root node A does not have left subtree. Notice &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-302","post","type-post","status-publish","format-standard","hentry","category-trees","entry"],"_links":{"self":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":1,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":444,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/302\/revisions\/444"}],"wp:attachment":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}