Binary Tree Right Side View
Explore how to determine the right side view of a binary tree by implementing a depth-first search in JavaScript. Learn to traverse the tree level by level from the rightmost node first and understand the time and space complexity of this approach.
We'll cover the following...
We'll cover the following...
Description
You are given a binary tree T. Imagine you are standing to the right of it; you have to return the value of the nodes you can see from top to bottom.
You have to return the rightmost nodes on their respective levels in the form of an array.
Let’s discuss an example below:
Coding exercise
Binary tree right side view
Solution
We can use a depth-first search (DFS) to solve this problem. The intuition here is to traverse the tree level by level recursively, starting from the rightmost node for each recursive call.
Let’s review the implementation below:
Binary tree right side view
Complexity measures
| Time Complexity | Space |
|---|