404-Sum-of-Lef- Leaves
0x0 题目详情
3
/ \
9 20
/ \
15 70x1 解题思路
0x2 实现代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int result;
public int sumOfLeftLeaves(TreeNode root) {
if(root==null){
return 0;
}
if(root.left!=null){
//只在属于左子树的递归中计算结果
sumOfLeftLeaves(root.left);
if(root.left.left==null && root.left.right==null){
result+=root.left.val;
}
}
sumOfLeftLeaves(root.right);
return result;
}
}0x3 课后总结
Last updated