Open In App

Right View of a Binary Tree

Last Updated : 08 Oct, 2025
Comments
Improve
Suggest changes
65 Likes
Like
Report

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.

C++
#include <iostream>
#include<vector>
using namespace std;

// Node Structure
class Node {
public:
    int data;
    Node *left, *right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

// Function for the right view using Recursion
void recRightView(Node* root, int level, 
                        int& maxLevel, vector<int>& result) {
    if (!root) return;

    // If current level is more than max level,
    // this is the first node of that level
    if (level > maxLevel) {
        result.push_back(root->data);
        maxLevel = level;
    }

    // Traverse right subtree first, then left subtree
    recRightView(root->right, level + 1,
                       maxLevel, result);
    recRightView(root->left, level + 1,
                       maxLevel, result);
}

// Function to return the right view of the binary tree
vector<int> rightView(Node *root) {
    vector<int> result;
    int maxLevel = -1;

    // Start recursion with root at level 0
    recRightView(root, 0, maxLevel, result);
    
    return result;
}

int main() {
  
    // Representation of the input tree:
    //         1
    //        / \
    //       2   3
    //           / \   
    //          4   5  
    Node *root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->right->left = new Node(4);
    root->right->right = new Node(5);

    vector<int> result = rightView(root);
    
    for (int val : result) {
        cout << val << " ";
    }
    
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// Node Structure
struct Node {
    int data;
    struct Node *left, *right;
};

// function for the right view using Recursion
void recRightView(struct Node* root, int level, 
               int* maxLevel, int* result, int* index) {
    if (!root) return;

    // If current level is more than max level,
    // this is the first node of that level
    if (level > *maxLevel) {
        result[(*index)++] = root->data;
        *maxLevel = level;
    }

    // Traverse right subtree first, then 
    // left subtree
    recRightView(root->right, level + 1,
                       maxLevel, result, index);
    recRightView(root->left, level + 1, 
                       maxLevel, result, index);
}

// Function to return the right view of the binary tree
void rightView(struct Node* root, int* result, int* size) {
    int maxLevel = -1;
    int index = 0;

    // Start recursion with root at level 0
    recRightView(root, 0, &maxLevel,
                       result, &index);
    
    *size = index;
}

struct Node* createNode(int x) {
    struct Node* newNode 
      = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = x;
    newNode->left = newNode->right = NULL;
    return newNode;
}

int main() {
  
    // Representation of the input tree:
    //         1
    //        / \
    //       2   3
    //          / \   
    //         4   5  
    struct Node *root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->right->left = createNode(4);
    root->right->right = createNode(5);

    int result[100]; 
    int size = 0;

    rightView(root, result, &size);
    
    for (int i = 0; i < size; i++) {
        printf("%d ", result[i]);
    }
    
    return 0;
}
Java
import java.util.ArrayList;

// Node Structure
class Node {
    int data;
    Node left, right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {
    
    // function for the right view using Recursion
    static void recRightView(Node root, int level, 
                  int[] maxLevel, ArrayList<Integer> result) {
        if (root == null) return;

        // If current level is more than max level,
        // this is the first node of that level
        if (level > maxLevel[0]) {
            result.add(root.data);
            maxLevel[0] = level;
        }

        // Traverse right subtree first, then left subtree
        recRightView(root.right, level + 1,
                          maxLevel, result);
        recRightView(root.left, level + 1,
                          maxLevel, result);
    }

    // Function to return the right view of the binary tree
    static ArrayList<Integer> rightView(Node root) {
        ArrayList<Integer> result = new ArrayList<>();
        int[] maxLevel = new int[] {-1};
        
        // Start recursion with root at level 0
        recRightView(root, 0, maxLevel, result);
        
        return result;
    }

    public static void main(String[] args) {
      
        // Representation of the input tree:
        //         1
        //        / \
        //       2   3
        //           / \   
        //          4   5 
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.right.left = new Node(4);
        root.right.right = new Node(5);

        ArrayList<Integer> result = rightView(root);
        
        for (int val : result) {
            System.out.print(val + " ");
        }
    }
}
Python
# Node Structure
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

#  function for the right view using Recursion
def recRightView(root, level, maxLevel, result):
    if root is None:
        return

    # If current level is more than max level,
    # this is the first node of that level
    if level > maxLevel[0]:
        result.append(root.data)
        maxLevel[0] = level

    # Traverse right subtree first, then left subtree
    recRightView(root.right, level + 1, 
                       maxLevel, result)
    recRightView(root.left, level + 1,
                       maxLevel, result)

# Function to return the right view of the binary tree
def rightView(root):
    result = []
    maxLevel = [-1]
    
    # Start recursion with root at level 0
    recRightView(root, 0, maxLevel, result)
    
    return result

if __name__ == "__main__":
    
    # Representation of the input tree:
    #         1
    #        / \
    #       2   3
    #           / \   
    #          4   5 
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.right.left = Node(4)
    root.right.right = Node(5)

    result = rightView(root)
    
    for val in result:
        print(val, end=" ")
C#
using System;
using System.Collections.Generic;

// Node Structure
class Node {
    public int data;
    public Node left, right;

    public Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {
  
    // function for the right view using Recursion
    static void recRightView(Node root, int level,
                     ref int maxLevel, List<int> result) {
        if (root == null) return;

        // If current level is more than max level,
        // this is the first node of that level
        if (level > maxLevel) {
            result.Add(root.data);
            maxLevel = level;
        }

        // Traverse right subtree first, then left subtree
        recRightView(root.right, level + 1, 
                           ref maxLevel, result);
        recRightView(root.left, level + 1,
                           ref maxLevel, result);
    }

    // Function to return the right view of the binary tree
    static List<int> rightView(Node root) {
        List<int> result = new List<int>();
        int maxLevel = -1;

        // Start recursion with root at level 0
        recRightView(root, 0, ref maxLevel, result);

        return result;
    }

    static void Main(string[] args) {
      
        // Representation of the input tree:
        //         1
        //        / \
        //       2   3
        //           / \   
        //          4   5 
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.right.left = new Node(4);
        root.right.right = new Node(5);

        List<int> result = rightView(root);
        
        foreach (int val in result) {
            Console.Write(val + " ");
        }
        Console.WriteLine();
    }
}
JavaScript
// Node Structure
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// function for the right view using Recursion
function recRightView(root, level, maxLevel, result) {
    if (root === null) return;

    // If current level is more than max level,
    // this is the first node of that level
    if (level > maxLevel[0]) {
        result.push(root.data);
        maxLevel[0] = level;
    }

    // Traverse right subtree first, then left subtree
    recRightView(root.right, level + 1,
                                   maxLevel, result);
    recRightView(root.left, level + 1, 
                                   maxLevel, result);
}

// Function to return the right view of the binary tree
function rightView(root) {
    let result = [];
    let maxLevel = [-1];

    // Start recursion with root at level 0
    recRightView(root, 0, maxLevel, result);

    return result;
}


// Driver Code
// Representation of the input tree:
//         1
//        / \
//       2   3
//           / \   
//          4   5 

let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.right.left = new Node(4);
root.right.right = new Node(5);

let result = rightView(root);

console.log(result.join(' '));

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.

C++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

// Node Structure
class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int x)
    {
        data = x;
        left = right = nullptr;
    }
};

vector<int> rightView(Node *root)
{
    vector<int> result;

    if (root == nullptr)
        return result;

    // Queue for level order traversal
    queue<Node *> q;
    q.push(root);

    while (!q.empty())
    {

        // Number of nodes at current level
        int levelSize = q.size();

        for (int i = 0; i < levelSize; i++)
        {
            Node *curr = q.front();
            q.pop();

            // If it's the last node of the current level
            if (i == levelSize - 1)
            {
                result.push_back(curr->data);
            }

            // Enqueue left child
            if (curr->left != nullptr)
            {
                q.push(curr->left);
            }

            // Enqueue right child
            if (curr->right != nullptr)
            {
                q.push(curr->right);
            }
        }
    }

    return result;
}

int main()
{

    // Representation of the input tree:
    //         1
    //        / \
    //       2   3
    //          / \   
    //         4   5
    Node *root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->right->left = new Node(4);
    root->right->right = new Node(5);

    vector<int> result = rightView(root);

    for (int val : result)
    {
        cout << val << " ";
    }
    cout << endl;

    return 0;
}
Java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

// Node Structure
class Node {
    int data;
    Node left, right;

    Node(int x)
    {
        data = x;
        left = right = null;
    }
}

class GFG {

    static ArrayList<Integer> rightView(Node root)
    {
        ArrayList<Integer> result = new ArrayList<>();

        if (root == null) {
            return result;
        }

        // Queue for level order traversal
        Queue<Node> q = new LinkedList<>();
        q.add(root);

        while (!q.isEmpty()) {

            // Number of nodes at the current level
            int levelSize = q.size();

            for (int i = 0; i < levelSize; i++) {
                Node curr = q.poll();

                // If it's the last node of the current
                // level
                if (i == levelSize - 1) {
                    result.add(curr.data);
                }

                // Enqueue left child
                if (curr.left != null) {
                    q.add(curr.left);
                }

                // Enqueue right child
                if (curr.right != null) {
                    q.add(curr.right);
                }
            }
        }

        return result;
    }

    public static void main(String[] args)
    {

        // Representation of the input tree:
        //         1
        //        / \
        //       2   3
        //          / \   
        //         4   5
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.right.left = new Node(4);
        root.right.right = new Node(5);
        
        ArrayList<Integer> result = rightView(root);
        for (int val : result) {
            System.out.print(val + " ");
        }
        
        System.out.println();
    }
}
Python
from collections import deque

# Node Structure


class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

# Function to return the right view of the binary tree


def rightView(root):
    result = []

    if root is None:
        return result

    # Queue for level order traversal
    q = deque([root])

    while q:

        # Number of nodes at the current level
        level_size = len(q)

        for i in range(level_size):
            curr = q.popleft()

            # If it's the last node of the
            # current level
            if i == level_size - 1:
                result.append(curr.data)

            # Enqueue left child
            if curr.left is not None:
                q.append(curr.left)

            # Enqueue right child
            if curr.right is not None:
                q.append(curr.right)

    return result


if __name__ == "__main__":

    # Representation of the input tree:
    #         1
    #        / \
    #       2   3
    #          / \
    #         4   5
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.right.left = Node(4)
    root.right.right = Node(5)

    result = rightView(root)

    for val in result:
        print(val, end=" ")
C#
using System;
using System.Collections.Generic;

// Node Structure
class Node {
    public int data;
    public Node left, right;

    public Node(int x)
    {
        data = x;
        left = right = null;
    }
}

class GFG {

    static List<int> rightView(Node root)
    {
        List<int> result = new List<int>();

        if (root == null) {
            return result;
        }

        // Queue for level order traversal
        Queue<Node> queue = new Queue<Node>();
        queue.Enqueue(root);

        while (queue.Count > 0) {

            // Number of nodes at the current level
            int levelSize = queue.Count;

            for (int i = 0; i < levelSize; i++) {
                Node curr = queue.Dequeue();

                // If it's the last node of
                // the current level
                if (i == levelSize - 1) {
                    result.Add(curr.data);
                }

                // Enqueue left child
                if (curr.left != null) {
                    queue.Enqueue(curr.left);
                }

                // Enqueue right child
                if (curr.right != null) {
                    queue.Enqueue(curr.right);
                }
            }
        }

        return result;
    }

    static void Main(string[] args)
    {

        // Representation of the input tree:
        //         1
        //        / \
        //       2   3
        //           / \   
        //          4   5
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.right.left = new Node(4);
        root.right.right = new Node(5);

        List<int> result = rightView(root);

        foreach(int val in result)
        {
            Console.Write(val + " ");
        }
    }
}
JavaScript
// Node Structure
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Custom Queue Class
class Queue {
    constructor() {
        this.items = [];
        this.frontIndex = 0;
        this.backIndex = 0;
    }

    enqueue(item) {
        this.items[this.backIndex++] = item;
    }

    dequeue() {
        if (this.isEmpty()) return null;
        const item = this.items[this.frontIndex];
        delete this.items[this.frontIndex++];
        return item;
    }

    get length() {
        return this.backIndex - this.frontIndex;
    }

    isEmpty() {
        return this.length === 0;
    }
}

function rightView(root) {
    let result = [];

    if (root === null) {
        return result;
    }

    // Queue for level order traversal
    let queue = new Queue();
    queue.enqueue(root);

    while (queue.length > 0) {
    
        // Number of nodes at the current level
        let levelSize = queue.length;

        for (let i = 0; i < levelSize; i++) {
            let curr = queue.dequeue();

            // If it's the last node of the 
            // current level
            if (i === levelSize - 1) {
                result.push(curr.data);
            }

            // Enqueue left child
            if (curr.left !== null) {
                queue.enqueue(curr.left);
            }

            // Enqueue right child
            if (curr.right !== null) {
                queue.enqueue(curr.right);
            }
        }
    }

    return result;
}


// Driver Code
// Representation of the input tree:
//         1
//        / \
//       2   3
//           / \   
//          4   5 
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.right.left = new Node(4);
root.right.right = new Node(5);

let result = rightView(root);
console.log(result.join(' '));

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.
C++
#include <iostream>
#include <vector>
using namespace std;

// Node Structure
class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

vector<int> rightView(Node* root) {
    
    vector<int> res;  
   
    int level = 0;     

    while (root) {
        
        // If the node has a right child,
        // find the inorder predecessor
        if (root->right) {
            Node *pred = root->right; 
            int backDepth = 1; 
            
            // Find the leftmost node in the right subtree
            while (pred->left != nullptr && 
                                  pred->left != root) {
                pred = pred->left;
                backDepth++;
            }
            
            // If threading is not yet established
            if (pred->left == nullptr) {
              
                // Add the current node to the view if 
                // visiting the level for the first time
                if (res.size() == level) {
                    res.push_back(root->data);
                }
                
                // Establish the thread and move 
                // to the right subtree
                pred->left = root;
                root = root->right;
                level++;  
            }
            else {  
              
                // Threading was already done
                //(second visit) remove the thread and 
                // go to the left subtree
                pred->left = nullptr;
                root = root->left;
                level -= backDepth;  
            }
        }
        else {
          
            // If no right child, process the current 
            // node and move to the left child
            if (res.size() == level) {
                res.push_back(root->data);
            }
            
            root = root->left;
            level++;  
        }
    }
 
    return res;
}


int main() {
    
    // Representation of the input tree:
    //         1
    //        / \
    //       2   3
    //          / \   
    //         4   5 
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->right->left = new Node(4);
    root->right->right = new Node(5);

    vector<int> result = rightView(root);
    for (int val : result) {
        cout << val << " ";
    }
    
    cout << endl;
    
    return 0;
}
Java
import java.util.ArrayList;

// Node Structure
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    static ArrayList<Integer> rightView(Node root) {
 
        ArrayList<Integer> res = new ArrayList<>();  

        int level = 0;     
 
        while (root != null) {
            
            // If the node has a right child,
            // find the inorder predecessor
            if (root.right != null) {
                Node pred = root.right; 
                int backDepth = 1; 
                
                // Find the leftmost node in the right subtree
                while (pred.left != null && pred.left != root) {
                    pred = pred.left;
                    backDepth++;
                }
                
                // If threading is not yet established
                if (pred.left == null) {
                  
                    // Add the current node to the view if 
                    // visiting the level for the first time
                    if (res.size() == level) {
                        res.add(root.data);
                    }
                    
                    // Establish the thread and move 
                    // to the right subtree
                    pred.left = root;
                    root = root.right;
                    level++;  
                }
                else {  
                  
                    // Threading was already done
                    //(second visit) remove the thread and 
                    // go to the left subtree
                    pred.left = null;
                    root = root.left;
                    level -= backDepth;  
                }
            }
            else {
              
                // If no right child, process the current 
                // node and move to the left child
                if (res.size() == level) {
                    res.add(root.data);
                }
                
                root = root.left;
                level++;  
            }
        }
 
        return res;
    }

    public static void main(String[] args) {
        
        // Representation of the input tree:
        //         1
        //        / \
        //       2   3
        //          / \   
        //         4   5 
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.right.left = new Node(4);
        root.right.right = new Node(5);

        ArrayList<Integer> result = rightView(root);
        for (int val : result) {
            System.out.print(val + " ");
        }
        
        System.out.println();
    }
}
Python
# Node Structure
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None

def rightView(root):

    res = []

    level = 0

    while root:
        
        # If the node has a right child,
        # find the inorder predecessor
        if root.right:
            pred = root.right
            backDepth = 1

            # Find the leftmost node in the right subtree
            while pred.left is not None and pred.left != root:
                pred = pred.left
                backDepth += 1

            # If threading is not yet established
            if pred.left is None:
                
                # Add the current node to the view if
                # visiting the level for the first time
                if len(res) == level:
                    res.append(root.data)
                
                # Establish the thread and move
                # to the right subtree
                pred.left = root
                root = root.right
                level += 1
            else:
                
                # Threading was already done
                # (second visit) remove the thread and
                # go to the left subtree
                pred.left = None
                root = root.left
                level -= backDepth
        else:
            
            # If no right child, process the current
            # node and move to the left child
            if len(res) == level:
                res.append(root.data)
            
            root = root.left
            level += 1
    
    return res


if __name__ == "__main__":
    # Representation of the input tree:
    #         1
    #        / \
    #       2   3
    #          / \
    #         4   5
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.right.left = Node(4)
    root.right.right = Node(5)
    
    result = rightView(root)
    print(" ".join(map(str, result)))
C#
using System;
using System.Collections.Generic;

// Node Structure
class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    static List<int> rightView(Node root) {

        List<int> res = new List<int>();  

        int level = 0;     

        while (root != null) {
            
            // If the node has a right child,
            // find the inorder predecessor
            if (root.right != null) {
                Node pred = root.right; 
                int backDepth = 1; 
                
                // Find the leftmost node in the right subtree
                while (pred.left != null && pred.left != root) {
                    pred = pred.left;
                    backDepth++;
                }
                
                // If threading is not yet established
                if (pred.left == null) {
                  
                    // Add the current node to the view if 
                    // visiting the level for the first time
                    if (res.Count == level) {
                        res.Add(root.data);
                    }
                    
                    // Establish the thread and move 
                    // to the right subtree
                    pred.left = root;
                    root = root.right;
                    level++;  
                }
                else {  
                  
                    // Threading was already done
                    //(second visit) remove the thread and 
                    // go to the left subtree
                    pred.left = null;
                    root = root.left;
                    level -= backDepth;  
                }
            }
            else {
              
                // If no right child, process the current 
                // node and move to the left child
                if (res.Count == level) {
                    res.Add(root.data);
                }
                
                root = root.left;
                level++;  
            }
        }

        return res;
    }

    static void Main(string[] args) {
        
        // Representation of the input tree:
        //         1
        //        / \
        //       2   3
        //          / \   
        //         4   5 
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.right.left = new Node(4);
        root.right.right = new Node(5);

        List<int> result = rightView(root);
        foreach (int val in result) {
            Console.Write(val + " ");
        }
        
        Console.WriteLine();
    }
}
JavaScript
// Node Structure
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

function rightView(root) {

    const res = [];  

    let level = 0;     

    while (root) {
        
        // If the node has a right child,
        // find the inorder predecessor
        if (root.right) {
            let pred = root.right; 
            let backDepth = 1; 
            
            // Find the leftmost node in the right subtree
            while (pred.left !== null && pred.left !== root) {
                pred = pred.left;
                backDepth++;
            }
            
            // If threading is not yet established
            if (pred.left === null) {
              
                // Add the current node to the view if 
                // visiting the level for the first time
                if (res.length === level) {
                    res.push(root.data);
                }
                
                // Establish the thread and move 
                // to the right subtree
                pred.left = root;
                root = root.right;
                level++;  
            }
            else {  
              
                // Threading was already done
                //(second visit) remove the thread and 
                // go to the left subtree
                pred.left = null;
                root = root.left;
                level -= backDepth;  
            }
        }
        else {
          
            // If no right child, process the current 
            // node and move to the left child
            if (res.length === level) {
                res.push(root.data);
            }
            
            root = root.left;
            level++;  
        }
    }

    return res;
}

// Driver code
// Representation of the input tree:
//         1
//        / \
//       2   3
//          / \   
//         4   5 
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.right.left = new Node(4);
root.right.right = new Node(5);

const result = rightView(root);
console.log(result.join(' '));

Output
1 3 5 

Print Right View of a Binary Tree

Explore