PIXELBANKv9.1.0
Menu

Binary Tree Right Side View

Given the root of a binary tree (as a level-order array), return the values of the nodes you can see when looking at the tree from the right side (top to bottom).

Output as space-separated integers.

Example:

Input:
1,2,3,null,5,null,4
Output:
1 3 4
Reasoning:
  • The binary tree is constructed from the level-order array: the first element 1 is the root, then its children 2 and 3, followed by their children, with null indicating no child.
  • The tree structure is:
    • Level 1: 1
    • Level 2: 2, 3
    • Level 3: 5 (child of 2), 4 (child of 3)
  • We traverse the tree level by level from right to left, selecting the last node at each level: 1 (level 1), 3 (level 2), 4 (level 3).
  • The selected node values form the right side view of the tree, which are output as space-separated integers: 1 3 4.

Constraints:

  • 0 <= number of nodes <= 100
  • -100 <= Node.val <= 100
solution.py

Test Results

0/0
Run code to see test results.