Back

Binary Tree Right Side View

medium

Given the root of a binary tree, imagine yourself standing on the right side of it. Return the values of the nodes you can see, ordered from top to bottom.

Test Cases

Copy an input into the main harness and Run to verify
Input
root = [1,2,3,null,5,null,4]
Expected Output
[1,3,4]
Input
root = [1,null,3]
Expected Output
[1,3]
Input
root = []
Expected Output
[]

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Hints

Hint 1 — click to reveal

You are looking for the last node on each level, not the rightmost branch — a right child may be missing.

Hint 2 — click to reveal

Level-order traversal makes 'last node on this level' trivial.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.