Simple Java recursion beats 98%


  • 0
    L
        private int max;
        private TreeNode node;
        public int findBottomLeftValue(TreeNode root) {
            node = root;
            helper(root,0);
            return node.val;
        }
        
        private void helper(TreeNode root, int height) {
            if(root.left==null && root.right==null) {
                if(height>max) {
                    max = height;
                    node = root;
                }
            }
            
            if(root.left!=null) {
                helper(root.left,height+1);
            }
            if(root.right!=null) {
                helper(root.right,height+1);
            }
        }
    

Log in to reply
 

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