class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null){
return true;
}else {
return isBalanced(root.left) && isBalanced(root.right) && Math.abs(height(root.left)-height(root.right))<=1;
}
}
public static int height(TreeNode root){
if (root == null){
return 0;
}else {
return Math.max(height(root.left), height(root.right))+1;
}
}
}
leetcode 110
最新推荐文章于 2024-07-12 18:12:33 发布