package daily20201222;
/**
* @author : zhaoliang
* @program :newCoder
* @description : 二叉树的左叶子之和
* @create : 2020/12/22 15:44
*/
public class SumOfLeftLeaves {
class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x){
this.val = x;
}
}
public int sumOfLeftLeaves(TreeNode root){
if (root==null)return 0;
int sum =0;
if(root.left!=null){
TreeNode left = root.left;
if(left.left==null && left.right==null){
sum+=left.val;
}else {
sum+=sumOfLeftLeaves(left);
}
}
if (root.right!=null){
sum+=sumOfLeftLeaves(root.right);
}
return sum;
}
}
二叉树的左叶子之和
最新推荐文章于 2022-08-04 09:39:06 发布