1. /**
    2. * 给定一棵树的头节点,判断这棵树是否为平衡二叉树
    3. * 平衡二叉树定义:
    4. * 每一颗子数左右子树的高高度差不能超过1
    5. */
    6. public class Code03_IsBalanced {
    7. public static class Node {
    8. public int value;
    9. public Node left;
    10. public Node right;
    11. public Node(int data) {
    12. this.value = data;
    13. }
    14. }
    15. public static boolean isBalanced1(Node head) {
    16. boolean[] ans = new boolean[1];
    17. ans[0] = true;
    18. process1(head, ans);
    19. return ans[0];
    20. }
    21. public static int process1(Node head, boolean[] ans) {
    22. if (!ans[0] || head == null) {
    23. return -1;
    24. }
    25. int leftHeight = process1(head.left, ans);
    26. int rightHeight = process1(head.right, ans);
    27. if (Math.abs(leftHeight - rightHeight) > 1) {
    28. ans[0] = false;
    29. }
    30. return Math.max(leftHeight, rightHeight) + 1;
    31. }
    32. /**
    33. * 主函数
    34. * @param head
    35. * @return
    36. */
    37. public static boolean isBalanced2(Node head) {
    38. return process(head).isBalanced;
    39. }
    40. /**
    41. * 信息体类
    42. */
    43. public static class Info {
    44. public boolean isBalanced;
    45. public int height;
    46. public Info(boolean i, int h) {
    47. isBalanced = i;
    48. height = h;
    49. }
    50. }
    51. /**
    52. * 递归过程
    53. * @param x
    54. */
    55. public static Info process(Node x) {
    56. if (x == null) {
    57. return new Info(true, 0);
    58. }
    59. Info leftInfo = process(x.left);
    60. Info rightInfo = process(x.right);
    61. // 树的高度
    62. int height = Math.max(leftInfo.height, rightInfo.height) + 1;
    63. // 先认为自己是平衡的,然后看下面条件是否违反。
    64. boolean isBalanced = true;
    65. if (!leftInfo.isBalanced) { // 如果左树不平,不平
    66. isBalanced = false;
    67. }
    68. if (!rightInfo.isBalanced) { // 如果右树不平,不平
    69. isBalanced = false;
    70. }
    71. if (Math.abs(leftInfo.height - rightInfo.height) > 1) { // 左右两侧树的高度差》1 不平
    72. isBalanced = false;
    73. }
    74. return new Info(isBalanced, height);
    75. }
    76. // for test
    77. public static Node generateRandomBST(int maxLevel, int maxValue) {
    78. return generate(1, maxLevel, maxValue);
    79. }
    80. // for test
    81. public static Node generate(int level, int maxLevel, int maxValue) {
    82. if (level > maxLevel || Math.random() < 0.5) {
    83. return null;
    84. }
    85. Node head = new Node((int) (Math.random() * maxValue));
    86. head.left = generate(level + 1, maxLevel, maxValue);
    87. head.right = generate(level + 1, maxLevel, maxValue);
    88. return head;
    89. }
    90. public static void main(String[] args) {
    91. int maxLevel = 5;
    92. int maxValue = 100;
    93. int testTimes = 1000000;
    94. for (int i = 0; i < testTimes; i++) {
    95. Node head = generateRandomBST(maxLevel, maxValue);
    96. if (isBalanced1(head) != isBalanced2(head)) {
    97. System.out.println("Oops!");
    98. }
    99. }
    100. System.out.println("finish!");
    101. }
    102. }