# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def depth(self,root): #计算出子树高度
if not root: return 0
return max(self.depth(root.left),self.depth(root.right))+1
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root: return True
# ld=self.depth(root.left)
# rd=self.depth(root.right)
# if not (ld-rd<=1 or rd-ld<=1): #有测试用例不通过?
# return False
return abs(self.depth(root.left)-self.depth(root.right))<=1 and self.isBalanced(root.left) and self.isBalanced(root.right) #保证左右子树的高度差不大于1,继续遍历左右子树是否为二叉平衡树
自底向上,剪枝
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def depth(self,root):
if not root: return 0
ld=self.depth(root.left)
rd=self.depth(root.right)
if abs(ld-rd)>1: #左右子树深度>1,剪枝,直接返回-1
return -1
return max(ld,rd)+1 #计算左右子树的深度
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root: return True
if self.depth(root)==-1: return False # 直接返回
return self.isBalanced(root.left) and self.isBalanced(root.right) #继续判断左右子树
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
int l = calculateDepth(root.left);
int r = calculateDepth(root.right);
return Math.abs(l - r) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
// 二叉树的深度
public int calculateDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = calculateDepth(root.left);
int right = calculateDepth(root.right);
return Math.max(left, right) + 1;
}
}
// 时间复杂度是O(nlogn),空间是O(n)
第二种
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
if (calculateDepth(root) == -1) {
return false;
}
return true;
}
// 二叉树的深度
public int calculateDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = calculateDepth(root.left);
if (left == -1)
return -1;
int right = calculateDepth(root.right);
if (right == -1)
return -1;
// 减少递归次数
if (Math.abs(left - right) > 1) {
return -1;
}
return Math.max(left, right) + 1;
}
}
// 时间复杂度是O(n),空间是O(n)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
def recur(root):
if not root:
return 0
left = recur(root.left)
if left == -1:
return -1
right = recur(root.right)
if right == -1:
return -1
if abs(left - right) > 1:
return -1
return max(left, right) + 1
result = recur(root)
return result != -1
class Solution:
def calculateDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
left = self.calculateDepth(root.left)
if left == -1:
return -1
right = self.calculateDepth(root.right)
if right == -1:
return -1
if abs(left - right) > 1:
return -1
# remember to +1 so that the value can increase.
return max(left, right) + 1
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if root is None:
return True
if self.calculateDepth(root) == -1:
return False
return True
共以下 26 个回答
自顶向下
自底向上,剪枝
第一种
第二种
递归:时间O(N), 空间O(N)
递归
递归
class Solution {
public:
int calculateDepth(TreeNode* root) {
if (!root)
return 0;
return max(calculateDepth(root->left), calculateDepth(root->right)) + 1;
}
bool isBalanced(TreeNode* root) {
if(!root) return true;
if(abs(calculateDepth(root->left)-calculateDepth(root->right))<=1){
if(isBalanced(root->left)&&isBalanced(root->right)){
return true;
}
}
return false;
};
这个时间复杂度是?
题解
方法 递归 但是加一下记忆的方式来减少重复计算的次数,子树如果已经判断不平衡则直接return -1
time O(n) space O(n)
时间复杂度O(n),空间复杂度O(h)
时间复杂度:O(n)
空间复杂度:O(n)
递归
/**
* 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 int Depth(TreeNode root){
if(root null) return 0;
return 1 + Math.max(Depth(root.left), Depth(root.right));
}
}
思路:学习题解里别人的解法。主体是以上一题的计算深度为主,然后计算过程中通过后续遍历一次次比较左右树深度差是否大于1
时间on 空间on
思路: 计算当前结点字数深度 并判断 是否满足
//时间复杂度O(N2),空间复杂度为O(N)
public boolean isBalanced(TreeNode root) {
return checkBalance(root) != -1;
思路为递归判断二叉树的各个子树是否为平衡二叉树
时间复杂度为O(NlogN),递归判断子树是否是平衡二叉树时间复杂度为O(logN),递归返回二叉树的最大深度时间复杂度为O(N)。
空间复杂度为O(N)。
思路:两种方法,都是利用递归的方式,一种是检查以当前节点为根节点的二叉树是否是平衡二叉树,并递归的检查其左右子树是否平衡;另一种就是在计算每个节点其左右子树的最大深度的过程中判断是否满足平衡二叉树的要求。
时间复杂度:O(n)
空间复杂度:O(n)
递归后序遍历+剪枝
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public boolean isBalanced(TreeNode root) {
if(root null){
return true;
}
if(Math.abs(getDepth(root.left) – getDepth(root.right)) > 1){
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
}
时间:O(n)
空间:O(n)
打卡,先递归求深度,再利用性质。
时间复杂度,空间复杂度O(n)