My Java solution


  • 0
    L
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            return sumOfLeaves(root, false);
        }
        
        public int sumOfLeaves(TreeNode root, boolean left) {
            if(root == null) return 0;
            if(root.left == null && root.right == null) {
                if(left) return root.val;
                else return 0;
            }
            int leftLeft = 0, rightLeft = 0;
            if(root.left != null) leftLeft = sumOfLeaves(root.left, true);
            if(root.right != null) rightLeft = sumOfLeaves(root.right, false);
            return leftLeft+rightLeft;
        }
    }
    

Log in to reply
 

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