class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int leftheight = maxDepth(root.left);
int rightheight = maxDepth(root.right);
return leftheight>rightheight ? leftheight+1:rightheight+1;
}
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
if(Math.abs(maxDepth(root.left)-maxDepth(root.right))<=1){
return isBalanced(root.left)&&isBalanced(root.right);
}
return false;
}
}
给定一个二叉树,判断它是否是高度平衡的二叉树。 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
最新推荐文章于 2022-11-10 22:05:19 发布