share my java solution!


  • 0
    C
    public class Solution {
        private int res=0;
        public int findBottomLeftValue(TreeNode root) {
            int h=height(root);
            //System.out.println(h);
            findleftmost(root,1,h);
            return res;
        }
        
        public boolean findleftmost(TreeNode root,int level,int h){
            if(level==h){
                res=root.val;
                return true;
            }
            
            if(root.left!=null&&findleftmost(root.left,level+1,h)){
                return true;
            }
            
            if(root.right!=null&&findleftmost(root.right,level+1,h)){
                return true;
            }
            
            return false;
        
            
        }
        
        public int height(TreeNode root){
            if(root==null) return 0;
            return 1+Math.max(height(root.left),height(root.right));
        }
    }
    
    

Log in to reply
 

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