AlgoDaily

Right Node View of Binary Tree

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Question

A binary search tree is a data structure that has the following properties, - Each node in the tree has only two children. - The left subtree consists of nodes with values less than the root node. - The right subtree consists of nodes with values greater than the root node.

Consider a binary tree with the root node given as root. Your task is to return the values of the nodes that are on the right side of the binary tree, ordered from top to bottom.

image

For example, consider the tree in the image above. On the right side of the tree, we see the values 9, 11, and 16. These are returned as an array [9, 11, 16].

Constraints

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

CONTINUE WITH ALGODAILY PREMIUM

Keep going with the complete tutorial.

Unlock the remaining explanations and exercises, alongside the full learning library.

Explore Premium →
Open interactive view →