## 三、解题代码

public class Test {

private static class BinaryTreeNode {
int val;
BinaryTreeNode left;
BinaryTreeNode right;

public BinaryTreeNode() {
}

public BinaryTreeNode(int val) {
this.val = val;
}
}

public static int treeDepth(BinaryTreeNode root) {
if (root == null) {
return 0;
}

int left = treeDepth(root.left);
int right = treeDepth(root.right);

return left > right ? (left + 1) : (right + 1);
}

/**
* 判断是否是平衡二叉树，第一种解法
*
* @param root
* @return
*/
public static boolean isBalanced(BinaryTreeNode root) {
if (root == null) {
return true;
}

int left = treeDepth(root.left);
int right = treeDepth(root.right);
int diff = left - right;
if (diff > 1 || diff < -1) {
return false;
}

return isBalanced(root.left) && isBalanced(root.right);
}

/**
* 判断是否是平衡二叉树，第二种解法
*
* @param root
* @return
*/
public static boolean isBalanced2(BinaryTreeNode root) {
int[] depth = new int[1];
return isBalancedHelper(root, depth);
}

public static boolean isBalancedHelper(BinaryTreeNode root, int[] depth) {
if (root == null) {
depth[0] = 0;
return true;
}

int[] left = new int[1];
int[] right = new int[1];

if (isBalancedHelper(root.left, left) && isBalancedHelper(root.right, right)) {
int diff = left[0] - right[0];
if (diff >= -1 && diff <= 1) {
depth[0] = 1 + (left[0] > right[0] ? left[0] : right[0]);
return true;
}
}

return false;
}
}