Java, recursion solution


  • 0
    M
    public int sumOfLeftLeaves(TreeNode root) {
    		Total_sum ts = new Total_sum();
    		return sumOfLeftLeaves_m(root, ts);
        }
        
        private int sumOfLeftLeaves_m(TreeNode root, Total_sum ts) {
    		// TODO Auto-generated method stub
    		
    		if(root==null)
    			return 0;
    		
    		if(root.left!=null && root.left.left==null && root.left.right==null)     
    			ts.sum += root.left.val;
    		sumOfLeftLeaves_m(root.left,ts);
    		sumOfLeftLeaves_m(root.right,ts);
    
    		return ts.sum;
    
    	}
        
        public static class Total_sum {
    		private int sum=0;
    	}
    

Log in to reply
 

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