Binary Tree Right Side View

Given the root of a binary tree, return the values of the nodes visible from the right side, from top to bottom. Pattern focus: Preorder DFS (Root First). Visit the right subtree before the left subtree to capture the first visible node at each depth.

Input Format

root = binary tree root

Output Format

values visible from the right side

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^4 <= node values <= 10^4
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [1,2,3,null,5,null,4]

Output:

[1,3,4]

Explanation:

At each level, the rightmost node is visible.

Example 2:

Input:

root = [1,null,3]

Output:

[1,3]

Explanation:

The rightmost path is directly visible.

Loading...
Binary Tree Right Side View - Binary Tree