Intuitive Java solution


  • 0
    V
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            
            if (root == null) {
                return 0;
            }
            int val = isLeaf(root.left) ? root.left.val : 0;
            return val + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
        }
        
        public boolean isLeaf(TreeNode n){
            if (n == null) { return false; }
            return n.left == null && n.right == null;
        }
    }
    

Log in to reply
 

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