Solution Review: Problem Challenge 2
We'll cover the following
Right View of a Binary Tree (easy) #
Given a binary tree, return an array containing nodes in its right view. The right view of a binary tree is the set of nodes visible when the tree is seen from the right side.
Solution #
This problem follows the Binary Tree Level Order Traversal pattern. We can follow the same BFS approach. The only additional thing we will be do is to append the last node of each level to the result array.
Code #
Here is what our algorithm will look like; only the highlighted lines have changed:
Time complexity #
The time complexity of the above algorithm is , where āNā is the total number of nodes in the tree. This is due to the fact that we traverse each node once.
Space complexity #
The space complexity of the above algorithm will be as we need to return a list containing the level order traversal. We will also need space for the queue. Since we can have a maximum of nodes at any level (this could happen only at the lowest level), therefore we will need space to store them in the queue.
Similar Questions #
Problem 1: Given a binary tree, return an array containing nodes in its left view. The left view of a binary tree is the set of nodes visible when the tree is seen from the left side.
Solution: We will be following a similar approach, but instead of appending the last element of each level we will be appending the first element of each level to the output array.