1. public static class Node {
    2. public int value;
    3. public Node left;
    4. public Node right;
    5. public Node(int data) {
    6. this.value = data;
    7. }
    8. }
    9. public static boolean isBalanced1(Node head) {
    10. boolean[] ans = new boolean[1];
    11. // 假设是true
    12. ans[0] = true;
    13. process1(head, ans);
    14. return ans[0];
    15. }
    16. public static int process1(Node head, boolean[] ans) {
    17. if (!ans[0] || head == null) {
    18. return -1;
    19. }
    20. // 左树要信息
    21. int leftHeight = process1(head.left, ans);
    22. // 右树要信息
    23. int rightHeight = process1(head.right, ans);
    24. // 左高-右高>1,则false
    25. if (Math.abs(leftHeight - rightHeight) > 1) {
    26. ans[0] = false;
    27. }
    28. return Math.max(leftHeight, rightHeight) + 1;
    29. }