Leetcode 199 Binary Tree Right Side View Solution in java | Hindi Coding Community

0

 


Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

class Solution {
   
    private int maxL = 0;
   
    private List<Integer> rViewCalc(List<Integer> ls, TreeNode root, int level) {
       
        if(root == null)
            return ls;
       
        if(maxL < level) {
           
            ls.add(root.val);
            maxL = level;
        }
       
        rViewCalc(ls, root.right, level + 1);
        rViewCalc(ls, root.left, level + 1);
       
        return ls;        
    }
   
    public List<Integer> rightSideView(TreeNode root) {
       
        List<Integer> rView = new ArrayList<Integer>();
       
        return rViewCalc(rView, root, 1);
       
    }
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !