java recursive solution


  • 0
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            if (root == null) return 0;
    
            int result = sumOfLeftLeaves(root.right);
            if (root.left != null) {
                if (root.left.left == null && root.left.right == null) {
                    result += root.left.val;
                }
                else {
                    result += sumOfLeftLeaves(root.left);
                }
            }
    
            return result;
        }
    }
    

Log in to reply
 

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