Java recursive solution


  • 0
    M
    public class Solution {
        
        int sum = 0;
        
        public int sumOfLeftLeaves(TreeNode root) {
            sum(root, false);    
            return sum;
        }
        
        private void sum(TreeNode node, boolean isLeft) {
            if (node == null) return;
            sum(node.left, true);
            sum(node.right, false);
            if (isLeft && (node.left == null && node.right == null)) sum+=node.val;
        }
    }
    

Log in to reply
 

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