a comprehensible Java solution


  • 0
    A
    class Solution {
        int[] sum = new int[1];
        public int sumOfLeftLeaves(TreeNode root) {
            if(root==null) return 0;
            getSum(root,sum);
            return sum[0];
        }
        public void getSum(TreeNode root,int[] sum){
            if(root == null) return;
            if(root.left!=null && root.left.left==null && root.left.right==null){
                sum[0] += root.left.val;
            }
            getSum(root.left,sum);
            if(root.right!=null) getSum(root.right,sum);
        }
        
    }
    
    

Log in to reply
 

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