题目:输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过1 ,那么它就是一棵平衡二叉树。
代码如下:
package demo;
public class Test39_2 {
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);
}
/**
* 判断是否是平衡二叉树 解法1:在遍历树的每个结点的时候,调用函数treeDepth得到它的左右子树的深度。
* 如果每个结点的左右子树的深度相差都不超过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 (Math.abs(diff) > 1) {
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
}
/**
* 判断是否是平衡二叉树 解法2:每个结点只遍历一次。 用后序遍历的方式遍历二叉树的每一个结点,在遍历到一个结点之前我们就已经遍历了它的左右子树。
* 只要在遍历每个结点的时候记录它的深度(某一结点的深度等于它到叶子结点的路径的长度), 我们就可以一边遍历一边判断每个结点是不是平衡的。
*
* @param root
* @return
*/
public static boolean isBalanced2(BinaryTreeNode root) {
int[] depth = new int[1];
return isBalancedHelper(root, depth);
}
private 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 (Math.abs(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();
test5();
}
private static void test1() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(2);
BinaryTreeNode n3 = new BinaryTreeNode(3);
BinaryTreeNode n4 = new BinaryTreeNode(4);
BinaryTreeNode n5 = new BinaryTreeNode(5);
BinaryTreeNode n6 = new BinaryTreeNode(6);
BinaryTreeNode n7 = new BinaryTreeNode(7);
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
n3.left = n6;
n3.right = n7;
System.out.println("test1:完全二叉树:");
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println();
}
private static void test2() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(2);
BinaryTreeNode n3 = new BinaryTreeNode(3);
BinaryTreeNode n4 = new BinaryTreeNode(4);
BinaryTreeNode n5 = new BinaryTreeNode(5);
BinaryTreeNode n6 = new BinaryTreeNode(6);
BinaryTreeNode n7 = new BinaryTreeNode(7);
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
n3.right = n6;
n5.left = n7;
System.out.println("test2:不是完全二叉树,但是是普通的平衡二叉树:");
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println();
}
private static void test3() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(2);
BinaryTreeNode n3 = new BinaryTreeNode(3);
BinaryTreeNode n4 = new BinaryTreeNode(4);
BinaryTreeNode n5 = new BinaryTreeNode(5);
BinaryTreeNode n6 = new BinaryTreeNode(6);
BinaryTreeNode n7 = new BinaryTreeNode(7);
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
n5.left = n7;
System.out.println("test3:不是平衡二叉树:");
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println();
}
private static void test4() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(2);
BinaryTreeNode n3 = new BinaryTreeNode(3);
BinaryTreeNode n4 = new BinaryTreeNode(4);
BinaryTreeNode n5 = new BinaryTreeNode(5);
n1.left = n2;
n2.left = n3;
n3.left = n4;
n4.left = n5;
System.out.println("test4:只有左子树:");
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println();
}
private static void test5() {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(2);
BinaryTreeNode n3 = new BinaryTreeNode(3);
BinaryTreeNode n4 = new BinaryTreeNode(4);
BinaryTreeNode n5 = new BinaryTreeNode(5);
n1.right = n2;
n2.right = n3;
n3.right = n4;
n4.right = n5;
System.out.println("test5:只有右子树:");
System.out.println(isBalanced(n1));
System.out.println(isBalanced2(n1));
System.out.println();
}
}