https://leetcode.com/problems/sum-of-left-leaves/

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private int impl(TreeNode root, boolean left) {
        if (root == null) {
            return 0;
        }
        
        if (left && root.left == null && root.right == null) {
            return root.val;
        }
        
        return impl(root.left, true) + impl(root.right, false);
    }
    
    public int sumOfLeftLeaves(TreeNode root) {
        return impl(root, false);
    }
    
}

 

相关文章:

  • 2022-12-23
  • 2021-10-10
  • 2021-06-20
  • 2021-11-19
  • 2022-12-23
  • 2021-08-17
  • 2021-09-22
  • 2022-02-13
猜你喜欢
  • 2021-12-07
  • 2021-05-22
  • 2022-12-23
相关资源
相似解决方案