Simple Java Recursive Solution


  • 0
    K
    public class Solution {
        int sum = 0;
        public int sumOfLeftLeaves(TreeNode root) {
            if(root == null) return sum;
            sumOfLeftLeaves(root, null);
            return sum;
        }
        
        public void sumOfLeftLeaves(TreeNode root, String lr) {
            if(lr != null && root != null && root.left == null && root.right == null) sum+=root.val;
            if(root.left != null) sumOfLeftLeaves(root.left, "left");
            if(root.right != null) sumOfLeftLeaves(root.right, null);
        }
    }
    

Log in to reply
 

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