Java 1 ms DFS


  • 0
    V
        List<Integer> toRet;
        public List<Integer> rightSideView(TreeNode root) 
        {
            toRet = new ArrayList<Integer>();
            recMeth(root, 1);
            return toRet;
        }
        void recMeth(TreeNode root, int currHt)
        {
            if(root == null)
                return;
            if(toRet.size() < currHt)
                toRet.add(root.val);
            recMeth(root.right, currHt + 1);
            recMeth(root.left, currHt + 1);
        }

Log in to reply
 

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