110. 平衡二叉树
题目链接:https://programmercarl.com/0110.%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.html#%E9%A2%98%E5%A4%96%E8%AF%9D
递归。
当高度差大于1时,返回-1。
当某棵子树返回-1时,继续返回-1。
其他情况返回的是左右子树的最大高度+1
404. 左叶子之和
题目链接:https://programmercarl.com/0404.%E5%B7%A6%E5%8F%B6%E5%AD%90%E4%B9%8B%E5%92%8C.html
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int sum=0;
if(root==null||(root.right==null&&root.left==null))return 0;
if(root.left!=null&&(root.left.left==null&&root.left.right==null)){
sum+=root.left.val;
}
return sum+sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right);
}
}