Straightforward Java Solution


  • 1
    H
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            if (root == null) return 0;
            return getSum(root, false);
        }
        
        private int getSum(TreeNode node, boolean isLeft) {
            int res = 0;
            if (node.left == null && node.right == null) {
                res += isLeft ? node.val : 0;
                return res;
            }
            if (node.left != null) res += getSum(node.left, true);
            if (node.right != null) res += getSum(node.right, false);
            return res;
        }
    }
    

Log in to reply
 

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