Binary Tree Right Side View


  • 0

    Click here to see the full article post


  • 0
    A
    class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            
            dfs(root,res,1);
            
            return res;
        }
        
        public void dfs(TreeNode node,List<Integer> res,int level){
            if(node == null)
                return;
            if(level > res.size()){
                res.add(node.val);
            }
            dfs(node.right,res,level+1);
            dfs(node.left,res,level+1);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.