Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

118. Binary Tree Right Side View

MediumDay 35 of 60Trees & BST

Imagine standing on the right side of a binary tree and looking at it. Given its root, return the values of the nodes you can see, ordered from top to bottom. On each level you see only the rightmost node.

Example 1
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
Example 2
Input: root = [1,2,3,4,null,null,null,5]
Output: [1,3,4,5]
Level 2 and level 3 have nothing on the right side, so the left nodes 4 and 5 are visible.
Constraints
  • The number of nodes is in the range 0 to 100
  • -100 <= Node.val <= 100
Hints
Frequently asked at: Meta, Amazon, Bloomberg, Microsoft
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min