https://leetcode-cn.com/problems/balanced-binary-tree/
    给定一个二叉树,判断它是否是高度平衡的二叉树。

    本题中,一棵高度平衡二叉树定义为:

    一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。

    示例 1:
    image.png

    输入:root = [3,9,20,null,null,15,7]
    输出:true
    示例 2:
    image.png

    输入:root = [1,2,2,3,3,null,null,4,4]
    输出:false
    示例 3:
    输入:root = []
    输出:true
    判断出root.left和root.right的高度差以后,继续递归,直到root为空,return
    想明白递归return的条件和怎么递归

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public boolean isBalanced(TreeNode root) {
    18. if(root == null){
    19. return true;
    20. }
    21. if(Math.abs(maxDepth(root.left)-(maxDepth(root.right)))>1){
    22. return false;
    23. }
    24. return isBalanced(root.left)&&isBalanced(root.right);
    25. }
    26. public int maxDepth(TreeNode node){
    27. if(node == null){
    28. return 0;
    29. }
    30. int max = 0;
    31. int maxL = 0;
    32. int maxR = 0;
    33. maxL = maxDepth(node.left);
    34. maxR = maxDepth(node.right);
    35. return (maxL>maxR)?maxL+1:maxR+1;
    36. }
    37. }