/** * 给定一棵树的头节点,判断这棵树是否为平衡二叉树 * 平衡二叉树定义: * 每一颗子数左右子树的高高度差不能超过1 */public class Code03_IsBalanced { public static class Node { public int value; public Node left; public Node right; public Node(int data) { this.value = data; } } public static boolean isBalanced1(Node head) { boolean[] ans = new boolean[1]; ans[0] = true; process1(head, ans); return ans[0]; } public static int process1(Node head, boolean[] ans) { if (!ans[0] || head == null) { return -1; } int leftHeight = process1(head.left, ans); int rightHeight = process1(head.right, ans); if (Math.abs(leftHeight - rightHeight) > 1) { ans[0] = false; } return Math.max(leftHeight, rightHeight) + 1; } /** * 主函数 * @param head * @return */ public static boolean isBalanced2(Node head) { return process(head).isBalanced; } /** * 信息体类 */ public static class Info { public boolean isBalanced; public int height; public Info(boolean i, int h) { isBalanced = i; height = h; } } /** * 递归过程 * @param x */ public static Info process(Node x) { if (x == null) { return new Info(true, 0); } Info leftInfo = process(x.left); Info rightInfo = process(x.right); // 树的高度 int height = Math.max(leftInfo.height, rightInfo.height) + 1; // 先认为自己是平衡的,然后看下面条件是否违反。 boolean isBalanced = true; if (!leftInfo.isBalanced) { // 如果左树不平,不平 isBalanced = false; } if (!rightInfo.isBalanced) { // 如果右树不平,不平 isBalanced = false; } if (Math.abs(leftInfo.height - rightInfo.height) > 1) { // 左右两侧树的高度差》1 不平 isBalanced = false; } return new Info(isBalanced, height); } // for test public static Node generateRandomBST(int maxLevel, int maxValue) { return generate(1, maxLevel, maxValue); } // for test public static Node generate(int level, int maxLevel, int maxValue) { if (level > maxLevel || Math.random() < 0.5) { return null; } Node head = new Node((int) (Math.random() * maxValue)); head.left = generate(level + 1, maxLevel, maxValue); head.right = generate(level + 1, maxLevel, maxValue); return head; } public static void main(String[] args) { int maxLevel = 5; int maxValue = 100; int testTimes = 1000000; for (int i = 0; i < testTimes; i++) { Node head = generateRandomBST(maxLevel, maxValue); if (isBalanced1(head) != isBalanced2(head)) { System.out.println("Oops!"); } } System.out.println("finish!"); }}