/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int getDepth(TreeNode root){
if(root==null)return 0;
if(root.left==null&&root.right==null) return 1;
int left = getDepth(root.left);
int right = getDepth(root.right);
return Math.max(left,right)+1;
}
public boolean isBalanced(TreeNode root) {
if(root==null||root.left==null&&root.right==null){
return true;
}
boolean flag = isBalanced(root.left)&&isBalanced(root.right);
if(!flag) return false;
return Math.abs(getDepth(root.left)-getDepth(root.right))<=1;
}
}
检查平衡性(判断是否是AVL)
最新推荐文章于 2021-04-20 20:52:15 发布