Simple Java Solution!


  • 0
    S
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        private int sum = 0;
        public int sumOfLeftLeaves(TreeNode root) {
            helper(root, false);
            return sum;
        }
        
        private void helper(TreeNode root, boolean left) {
            if (root == null) {
                return;
            }
            if (root.left == null && root.right == null) {
                sum = left ? sum + root.val : sum;
                return;
            }
            helper(root.left, true);
            helper(root.right, false);
        }
    }
    

Log in to reply
 

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