Skip to content
BASA DSA

Binary Tree Right Side View

MediumTrees

Return the rightmost visible node value at each depth of a binary tree, starting at the root.

Example 1

Argument 1 tree: level-order input; omitted children are null.Argument 1 tree: level-order input; omitted children are null. Vertices: node 0: 1, node 1: 2, node 2: 3, node 3: 5, node 4: 4. Edges: node 0 (1) — node 1 (2) left child; node 0 (1) — node 2 (3) right child; node 1 (2) — node 3 (5) right child; node 2 (3) — node 4 (4) right child.12354
Argument 1 tree: level-order input; omitted children are null.

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

Explanation

The rightmost nodes at depths 0,1,2 are 1,3,4.

Example 2

Argument 1 tree: level-order input; omitted children are null.Argument 1 tree: level-order input; omitted children are null. Vertices: node 0: 1, node 1: 2, node 2: 3. Edges: node 0 (1) — node 1 (2) left child; node 1 (2) — node 2 (3) left child.123
Argument 1 tree: level-order input; omitted children are null.

Input
root = [1,2,null,3]
Output
[1,2,3]

Explanation

Without a right branch the left chain is still visible: 1,2,3.

Example 3

Argument 1 tree: empty tree (null root).
Input
root = []
Output
[]

Explanation

An empty tree contributes no visible nodes.

Constraints

  • 0 <= number of nodes <= 1000
  • Trees are breadth-first arrays with null holes

Function contractrightSideViewArguments: tree
Loading draft…
SolutionRuns on your device
Loading draft…