Binary Tree Right Side View
Explore how to return the right-side view of a binary tree by applying depth-first search methods. Learn to identify nodes visible from the right side and implement efficient solutions for tree traversal challenges.
We'll cover the following...
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:
-
n -
Node.data
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Binary Tree Right Side View
What will be the output of the following tree?
______ 1 ______
| |
2 _ _ 3
| |
_ 4 _ 5
| |
6 7
[1, 3, 5, 7]
[1, 3, 5]
[1, 4, 5, 7]
[1, 2, 3, 5]
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
Need a nudge?
Explore these hints—each one is designed to guide you a step closer to the solution.
package mainimport (. "golang-test-code/ds_v1/BinaryTree") // TreeNode[T]// Definition of a binary tree node// type TreeNode[T any] struct {// Data T// Left *TreeNode[T]// Right *TreeNode[T]// }// rightSideView returns the right side view of a binary treefunc rightSideView(root *TreeNode[int]) []int {// Replace this placeholder return statement with your codereturn make([]int, 0)}