0
点赞
收藏
分享

微信扫一扫

LC110——平衡二叉树

林塬 2022-03-15 阅读 98

官方题解: 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root ==null)
        {
            return true;
        }
        else
        {
     return Math.abs(getHeight(root.left)-getHeight(root.right))<=1&&isBalanced(root.left)&&isBalanced(root.right);
        }
    }
    public int  getHeight(TreeNode root)
    {
        if(root==null)
        {
            return 0;
        }else{
            return Math.max(getHeight(root.left),getHeight(root.right))+1;
        }
    }

  }
举报

相关推荐

0 条评论