Right View of Binary Tree | Practice | GeeksforGeeks

Given a Binary Tree, find Right view of it. Right view of a Binary Tree is set of nodes visible when tree is viewed from right side.
Right view of following tree is 1 3 7 8.
          1
       /     \
     2        3
   /   \      /    \
  4     5   6    7
    \
     8
Example 1:
Input:
       1
    /    \
   3      2
Output: 1 2

Example 2:
Input:
     10
    /   \
  20     30
 /   \
40  60 
Output: 10 30 60

Your Task:
Just complete the function rightView() that takes node as parameter and returns the right view as a list. 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 ≤ Number of nodes ≤ 105
1 ≤ Data of a node ≤ 105
0
binayshaw7777in 11 hours
Simple Java Solution:
class Solution{
    //Function to return list containing elements of right view of binary tree.
    ArrayList<Integer> rightView(Node root) {
      ArrayList<Integer> list = new ArrayList<>();
      RV(root, list, 0);
      return list;
    }
    
    public void RV(Node root, ArrayList<Integer> list, int level) {
        if (root == null) return;
        if (level == list.size()) list.add(root.data);
        RV(root.right, list, level+1);
        RV(root.left, list, level+1);
    }
}
0
tarunkanade21 hours ago
0.17/1.16 Java Theta(n), Theta(w)
ArrayList<Integer> rightView(Node root)
    {
      // Your code here
      ArrayList<Integer> list = new ArrayList<>();
      
      if(root == null){
          return list;
      }
      
      ArrayDeque<Node> q = new ArrayDeque<>();
      int count;
      Node cur;
      
      q.offer(root);
      
      while(!q.isEmpty()){
          count = q.size();
          
          for(int i=0; i<count; i++){
              cur = q.poll();
              if(i == count-1){
                  list.add(cur.data);
              }
              
              if(cur.left != null) q.offer(cur.left);
              if(cur.right != null) q.offer(cur.right);
          }
      }
      
      return list;
    }
0
shubham211019973 days ago
 ArrayList<Integer> rightView(Node root) {
       ArrayList<Integer> arr=new ArrayList<Integer>();
     if(root==null)return arr;
     Queue<Node>q=new LinkedList<>();
     q.add(root);
    while(q.isEmpty()==false){
        int size=q.size();
        for(int i=0;i<size;i++){
            Node curr=q.poll();
            if(i==size-1){
                arr.add(curr.data);
            }
            if(curr.left!=null)q.add(curr.left);
            if(curr.right!=null)q.add(curr.right);
        }
    }
    return arr;
   }
0
suyashsingh33 days ago
Java space: O(h) solution 
 ArrayList<Integer> rightView(Node node) {
 ArrayList<Integer> list=new ArrayList<Integer>();
 max=0;
 if(node==null) return list;
 max=1;
 rightd(node,1, list);
 
 return list;
 
}
static int max=0;
static void rightd(Node node,int level,ArrayList<Integer> list){
 if(node==null) return;
  max=Math.max(level, max);
 if(level==max) {//list.size()<=level so ll add right node after that level ll be equal 
  list.add(node.val); //don't need max param in this case
  
  max++;
 }
 

 
 rightd(node.right,level+1,list);
 rightd(node.left,level+1,list);
 
 
}
 
0
anugrahlko5 days ago
class Solution{
    //Function to return list containing elements of right view of binary tree.
    static HashMap<Integer, ArrayList<Integer>> map;
    ArrayList<Integer> rightView(Node node) {
        //add code here.
        int level =0;
        map = new HashMap<Integer, ArrayList<Integer>>();
        ArrayList<Integer> res = new ArrayList<Integer>();
        getMap(node, level);
        int i=0;
        while(map.containsKey(i)){
            ArrayList<Integer> l = map.get(i);
            int mostRightElementIndex = l.size()-1;
            int mostRightElement = l.get(mostRightElementIndex);
            res.add(mostRightElement);
            i++;
        }
        return res;
        
    }
    void getMap(Node node, Integer level){
        if(node==null) return;
        if(map.containsKey(level)){
            ArrayList<Integer> list = map.get(level);
            list.add(node.data);
            map.put(level,list);
        }else{
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(node.data);
            map.put(level,list);
        }
        getMap(node.left, level+1);
        getMap(node.right, level+1);
    }
}
+1
jainmuskan5652 weeks ago
 vector<int> v;   void fxn(Node * node,int level){       if(node==NULL){           return;       }       if(v.size()<= level){           v.push_back(node->data);       }       fxn(node->right,level+1);       fxn(node->left,level+1);   }   vector<int> rightView(Node *root)   {      // Your Code here      v.clear();      fxn(root, 0);      return v;   }
0
bheniavedant2 weeks ago
#User function Template for python3
 
'''# Node Class:class Node:   def init(self,val):       self.data = val       self.left = None       self.right = None'''class Solution:   #Function to return list containing elements of right view of binary tree.   def rightView(self,root):              # code here       level = []       result = []       if root is None:           return        q = [root]       while len(q)!= 0:           for node in q:               if node.left :                   level.append(node.left)               if node.right:                   level.append(node.right)                          result.append(node.data)           q = level           level = []
       return result             
0
rohanmeher1642 weeks ago
//Java solution
 
class Solution{   //Function to return list containing elements of right view of binary tree.   ArrayList<Integer> rightView(Node node) {       ArrayList<Integer> list=new ArrayList<Integer>();       helper(node,list);       return list;   }   void helper(Node root,ArrayList<Integer> list)   {       if(root==null) return;       Queue<Node> q=new LinkedList<Node>();       q.add(root);              while(!q.isEmpty())       {           int n=q.size();           for(int i=1;i<=n;i++)           {  Node t=q.poll();               if(i==n) list.add(t.data);                              if(t.left!=null) q.add(t.left);                if(t.right!=null) q.add(t.right);                           }       }   }}
+1
deathcloud
This comment was deleted.
+1
hanumanmanyam8373 weeks ago
class Solution{
    void left(Node root,ArrayList<Integer>res,int level)
    {
        if(root==null)
        {
            return;
        }
        if(res.size()==level)
        {
            res.add(root.data);
        }
        left(root.left,res,level+1);
        left(root.right,res,level+1);
    }
    //Function to return list containing elements of right view of binary tree.
    ArrayList<Integer> rightView(Node node) {
        //add code here.
        
        if(node!=null)
        {
            rightView(node.left);
            rightView(node.right);
            Node temp=node.left;
            node.left=node.right;
            node.right=temp;
        }
        Solution s= new Solution();
        ArrayList<Integer>res=new ArrayList<>();
        s.left(node,res,0);
        return res;
        
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.