Paths Subjects Questions Quizzes Pricing Search
Intermediate Open Pro

Rightmost Nodes of a Binary Tree

Given the root of a binary tree, imagine standing on the right side of it. Return the values of the nodes you can see, ordered from top to bottom — that is, for each level of the tree, return the value of the rightmost node at that level (this is often called the "right side view").

Example 1

Input: [1, 2, 3, null, 5, null, 4]

      1
     / \
    2   3
     \    \
      5    4

Output: [1, 3, 4]

Example 2

Input: [1, 2, 3, 4]

        1
       / \
      2   3
     /
    4

Output: [1, 3, 4] — level 2 has only node 4 (from the left subtree), and since it's the only node on that level, it counts as "rightmost."

Example 3

Input: [] → Output: []

Constraints

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

Share this question

← Back to Trees practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.