leetcode 0404 二叉树检查平衡性 DFS
public boolean isBalanced(TreeNode root) { if (root == null) { return true; } int diff = getDepth(root.left) - getDepth(root.right); diff = diff > 0 ? diff : diff * -1; if((diff <= 1)){return false;} return isBalanced(root.right) && isBalanced(root.left); } public int getDepth(TreeNode node) { if (node == null) { return 0; } return Math.max(getDepth(node.left), getDepth(node.right)) + 1; }
当你看清人们的真相,于是你知道了,你可以忍受孤独