题目
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sum=0;
public int sumOfLeftLeaves(TreeNode root) {
if(root==null)
return 0;
if(leaf(root.left))
return root.left.val+sumOfLeftLeaves(root.right);
return sumOfLeftLeaves(root.right)+sumOfLeftLeaves(root.left);
}
public boolean leaf(TreeNode node)
{
if(node==null)
return false;
return node.left==null&&node.right==null;
}
}
反思
1、认真看题,我就看错了,看成求左节点和