Solution: Binary Tree Right Side View

Let's solve the Binary Tree Right Side View problem using the Depth-First Search pattern.

Statement#

You are given a root of a binary tree that has n number of nodes. You have to return the right-side view in the form of a list.

A right-side view of a binary tree is the data of the nodes that are visible when the tree is viewed from the right side.

Constraints:

  • 00 \leq n 100\leq 100
  • 100-100 \leq Node.data 100\leq 100

Solution#

To return the list of the right-side view of the tree, we will apply a recursive depth-first search (DFS) approach. Our main function will first check if the root is NULL, in which case it returns an empty list. If the root is not NULL, we will initialize an empty list, rside, which will store the data of the tree’s rightmost nodes. Since we need only one right-side element at each level, the index of rside list will be maintained to keep track of these node values.

The recursive DFS() function will take three arguments as input, which are rside, node, and level, and check whether rside's length is equal to the current tree level. If this is TRUE, then add node's value to the list.

Next, we’ll iterate over node to check for any children. Here, the right child of the node will be visited first. If the child is not NULL, we’ll recursively call the DFS() function on that child, along with incrementing the level of the tree by 1. The purpose of visiting the right child first is that the rightmost elements of a level will be appended to the rside list, thereby increasing its length.

Finally, after completing the depth-first search, we will return the rside list, containing the right-side view of the tree.

Let’s look at the following illustration to get a better understanding of the solution:

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 rside level 0 rside.length 0 We will pass the root node to our function. Since the root is not NULL,the rside list will be created and DFS function will be called with theroot node, level = 0 and rside list.

1 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 level 0 rside.length 0 Since the (level == rside.length), we will append this node's data to the rside list. rside

2 of 15

Created with Fabric.js 3.6.6 level 0 rside 1 rside.length 1 1 2 3 4 5 6 7 8 9 Since the rside list have one node's data, rside.length will be changed to one.Now, the DFS function will be called with the right child of the root andthe level will be incremented.

3 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 level 1 rside.length 1 rside 1 Again (level == rside.length), we will append this node's data intothe rside list.

4 of 15

Created with Fabric.js 3.6.6 rside 1 3 level 2 rside.length 2 1 2 3 4 5 6 7 8 9 The length of the rside list will be changed to 2, and the DFS will becalled to its right child. We will increment the value of level.

5 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 level 2 rside.length 2 rside 1 3 Again, (level == rside.length) we will append this node's data intothe rside list.

6 of 15

Created with Fabric.js 3.6.6 level 2 rside 1 3 7 rside.length 3 1 2 3 4 5 6 7 8 9 The length of rside will be changed to 3. Now, DFS will be called tothe left child.

7 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 rside 1 3 7 level 2 rside.length 3 Since (level != rside.length), we will not append this node's data tothe rside list and return.

8 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 rside 1 3 7 level 1 rside.length 3 Since (level != rside.length), we will not append this node's data tothe rside list and return.

9 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 rside 1 3 7 level 2 rside.length 3 Yet again, (level != rside.length), we will not append this node's datato the rside list.

10 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 level 3 rside.length 3 rside 1 3 7 Since the (level == rside.length), we will append this node's data tothe rside list.

11 of 15

Created with Fabric.js 3.6.6 rside 1 3 7 9 level 3 rside.length 4 1 2 3 4 5 6 7 8 9 The length of rside will be changed to 4. Now, DFS will be calledto the left child.

12 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 rside 1 3 7 9 level 3 rside.length 4 Since (level != rside.length), we will not append this node's data tothe rside list and return.

13 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 rside 1 3 7 9 level 2 rside.length 4 Since (level != rside.length), we will not append this node's data tothe rside list and return.

14 of 15

Created with Fabric.js 3.6.6 1 2 3 4 5 6 7 8 9 rside 1 3 7 9 level 2 rside.length 4 Finally, all recursive calls of the depth-first search are complete.We will return the rside list. It contains the right-side view of the tree.

15 of 15

Let’s look at the code for this solution below:

main.py
BinaryTree.py
TreeNode.py
Binary Tree Right Side View

Time complexity#

The time complexity will be O(n)O(n), where nn is the number of nodes because each node is traversed once.

Space complexity#

The space complexity of this solution is O(h)O(h), where hh is the tree’s height.

Binary Tree Right Side View

Build Binary Tree from Preorder and Inorder Traversal