110.平衡二叉树
题目链接:110. 平衡二叉树 - 力扣(LeetCode)
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) {
return true;
}
if(Math.abs(height(root.left) - height(root.right)) > 1) {
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
}
private int height(TreeNode root) {
return root == null ? 0 : Math.max(height(root.left), height(root.right)) + 1;
}
}
257.二叉树的所有路径
题目链接:257. 二叉树的所有路径 - 力扣(LeetCode)
周末补
404.左叶子之和
题目链接:404. 左叶子之和 - 力扣(LeetCode)
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root == null) {
return 0;
}
if(root.left == null) {
return sumOfLeftLeaves(root.right);
}else if(root.left.left == null && root.left.right == null) {
return root.left.val + sumOfLeftLeaves(root.right);
}else {
return sumOfLeftLeaves(root.right) +sumOfLeftLeaves(root.left);
}
}
}