private static class BinaryTreeNode {
int val;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode() {
}
public BinaryTreeNode(int val) {
this.val = val;
}
}
如果一棵樹只有一個(gè)結(jié)點(diǎn),它的深度為。 如果根結(jié)點(diǎn)只有左子樹而沒有右子樹, 那么樹的深度應(yīng)該是其左子樹的深度加 1,同樣如果根結(jié)點(diǎn)只有右子樹而沒有左子樹,那么樹的深度應(yīng)該是其右子樹的深度加 1。如果既有右子樹又有左子樹, 那該樹的深度就是其左、右子樹深度的較大值再加 1。 比如在圖 6.1 的二叉樹中,根結(jié)點(diǎn)為 1 的樹有左右兩個(gè)子樹,其左右子樹的根結(jié)點(diǎn)分別為結(jié)點(diǎn) 2 和 3。根結(jié)點(diǎn)為 2 的左子樹的深度為 3, 而根結(jié)點(diǎn)為 3 的右子樹的深度為 2,因此根結(jié)點(diǎn)為 1 的樹的深度就是 4 。
這個(gè)思路用遞歸的方法很容易實(shí)現(xiàn), 只儒對(duì)遍歷的代碼稍作修改即可。
http://wiki.jikexueyuan.com/project/for-offer/images/56.png" alt="" />
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);
}
在遍歷樹的每個(gè)結(jié)點(diǎn)的時(shí)候,調(diào)用函數(shù) treeDepth 得到它的左右子樹的深度。如果每個(gè)結(jié)點(diǎn)的左右子樹的深度相差都不超過 1 ,按照定義它就是一棵平衡的二叉樹。
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);
}
用后序遍歷的方式遍歷二叉樹的每一個(gè)結(jié)點(diǎn),在遍歷到一個(gè)結(jié)點(diǎn)之前我們就已經(jīng)遍歷了它的左右子樹。只要在遍歷每個(gè)結(jié)點(diǎn)的時(shí)候記錄它的深度(某一結(jié)點(diǎn)的深度等于它到葉節(jié)點(diǎn)的路徑的長(zhǎng)度),我們就可以一邊遍歷一邊判斷每個(gè)結(jié)點(diǎn)是不是平衡的。
/**
* 判斷是否是平衡二叉樹,第二種解法
*
* @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;
}
public class Test39 {
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;
}
public static void main(String[] args) {
test1();
test2();
test3();
test4();
}
// 完全二叉樹
// 1
// / \
// 2 3
// /\ / \
// 4 5 6 7
private static void test1() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(1);
BinaryTreeNode n3 = new BinaryTreeNode(1);
BinaryTreeNode n4 = new BinaryTreeNode(1);
BinaryTreeNode n5 = new BinaryTreeNode(1);
BinaryTreeNode n6 = new BinaryTreeNode(1);
BinaryTreeNode n7 = new BinaryTreeNode(1);
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
n3.left = n6;
n3.right = n7;
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println("----------------");
}
// 不是完全二叉樹,但是平衡二叉樹
// 1
// / \
// 2 3
// /\ \
// 4 5 6
// /
// 7
private static void test2() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(1);
BinaryTreeNode n3 = new BinaryTreeNode(1);
BinaryTreeNode n4 = new BinaryTreeNode(1);
BinaryTreeNode n5 = new BinaryTreeNode(1);
BinaryTreeNode n6 = new BinaryTreeNode(1);
BinaryTreeNode n7 = new BinaryTreeNode(1);
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
n5.left = n7;
n3.right = n6;
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println("----------------");
}
// 不是平衡二叉樹
// 1
// / \
// 2 3
// /\
// 4 5
// /
// 7
private static void test3() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(1);
BinaryTreeNode n3 = new BinaryTreeNode(1);
BinaryTreeNode n4 = new BinaryTreeNode(1);
BinaryTreeNode n5 = new BinaryTreeNode(1);
BinaryTreeNode n6 = new BinaryTreeNode(1);
BinaryTreeNode n7 = new BinaryTreeNode(1);
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
n5.left = n7;
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println("----------------");
}
// 1
// /
// 2
// /
// 3
// /
// 4
// /
// 5
private static void test4() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(1);
BinaryTreeNode n3 = new BinaryTreeNode(1);
BinaryTreeNode n4 = new BinaryTreeNode(1);
BinaryTreeNode n5 = new BinaryTreeNode(1);
BinaryTreeNode n6 = new BinaryTreeNode(1);
BinaryTreeNode n7 = new BinaryTreeNode(1);
n1.left = n2;
n2.left = n3;
n3.left = n4;
n4.left = n5;
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println("----------------");
}
// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
private static void test5() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(1);
BinaryTreeNode n3 = new BinaryTreeNode(1);
BinaryTreeNode n4 = new BinaryTreeNode(1);
BinaryTreeNode n5 = new BinaryTreeNode(1);
BinaryTreeNode n6 = new BinaryTreeNode(1);
BinaryTreeNode n7 = new BinaryTreeNode(1);
n1.right = n2;
n2.right = n3;
n3.right = n4;
n4.right = n5;
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println("----------------");
}
}
http://wiki.jikexueyuan.com/project/for-offer/images/57.png" alt="" />