Solution
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalanced(TreeNode root) { if (root == null) { return true; } if (Math.abs(maxDepth(root.left) - maxDepth(root.right)) > 1) { return false; } return isBalanced(root.left) && isBalanced(root.right); } public int maxDepth(TreeNode root) { if (root == null) { return 0; } if (root.left == null && root.right == null) { return 1; } int depth = 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; } }Problem#2 * 没有领略到将false转换成数字-1的思想,其实不需要
Problem#1 * 思路错误,并不是左右子树都是平衡树,整棵树就是平衡树
