Easy to understand! recursive solution with flag to denote left leaves

  • 0
    public class Solution {
        public int sumOfLeftLeaves(TreeNode root){
          return sumOfLeftLeaves(root, false);
      public int sumOfLeftLeaves(TreeNode root,boolean isLeft){
          if (null == root)
              return 0;
          if (null == root.left && null == root.right && isLeft)
              return root.val;
          return sumOfLeftLeaves(root.left, true) + sumOfLeftLeaves(root.right, false);

Log in to reply

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