VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-right-view-binary-tree-2/

⇱ Right View of a Binary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Right View of a Binary Tree

Last Updated : 8 Oct, 2025

Given the root of a binary Tree, find the right view of the Binary Tree. The right view of a binary tree is a list of nodes you see when you look at the tree from the right side. It contains the rightmost node at each level, starting from the root (top level) down to the deepest level, in order.

Examples:

Input:

πŸ‘ file

Output: [1, 3, 4, 5]
Explanation: The Green colored nodes (1, 3, 4, 5) represents the Right view in the below Binary tree.

πŸ‘ 420046813222

Input:

πŸ‘ file

Output: [1, 3, 5]
Explanation: The Green colored nodes (1, 3, 5) represents the Right view in the below Binary tree

πŸ‘ 420046814

[Expected Approach - 1] Using Recursion

The idea is to traverse the complete tree recursively. Because we need the right view, At each step, we first go to the right subtree and then to the left subtree. For each level, we store the first node we encounter (the rightmost node for that level). By traversing the entire tree this way, we ensure that every level contributes its rightmost node to the result.


Output
1 3 5 

Time Complexity: O(n), We traverse all nodes of the binary tree exactly once.
Auxiliary Space: O(h), The space required for the recursion stack will be proportional to the height(h) of the tree.

[Expected Approach – 2] Using Level Order Traversal – O(n) Time and O(n) Space

The idea is to use level-order traversal to solve this problem. By traversing the tree level by level, we can store the last node of each level, which represents the rightmost node, to form the right view.


Output
1 3 5 

[Expected Approach - 3] Using Morris Traversal – O(n) Time and O(1) Space

The idea is to use Morris Traversal to print the right view of the binary tree by dynamically adjusting the tree's structure during traversal.

Follow the steps below to implement the idea:

  • Start with the root and keep moving while nodes exist.
  • If the current node has a right child, find its inorder predecessor (leftmost node of right subtree).
  • If visiting the predecessor for the first time:
    -> Record the current node’s value for the right view.
    -> Make a temporary thread from the predecessor back to the current node.
    -> Move to the right child.
  • If the predecessor already points back to the current node:
    -> Remove the thread.
    -> Move to the left child.
  • If the current node has no right child:
    -> Record its value if it’s the first node at this level.
    -> Move to the left child.
  • Repeat until all nodes are visited. The recorded values form the right view.

Output
1 3 5 
Comment