Simple Java Recursive solution (9ms)


  • 0
    M

    /**

    • Definition for a binary tree node.
    • public class TreeNode {
    • int val;
      
    • TreeNode left;
      
    • TreeNode right;
      
    • TreeNode(int x) { val = x; }
      
    • }
      */
      public class Solution {
      int sum = 0;
      public int sumOfLeftLeaves(TreeNode root) {
      if(root==null){
      return sum;
      }
      if(root.left!=null&&root.left.left==null&&root.left.right==null){
      sum+=root.left.val;
      }
      sumOfLeftLeaves(root.left);
      sumOfLeftLeaves(root.right);
      return sum;
      }
      }

Log in to reply
 

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