# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def isBalanced(self, root):
"""
:type root: Optional[TreeNode]
:rtype: bool
"""
if not root:
return True
if self.calculateDepth(root) == -1:
return False
return True
def calculateDepth(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
if not root:
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
return max(left, right) + 1
function isBalanced(root: TreeNode | null): boolean {
return getHeight(root) ==-1? false :true
function getHeight(root: TreeNode | null): number {
let res = 0 //平衡二叉树的深度
if (root === null) return 0; //如果无节点 返回0
let left = getHeight(root.left) //左节点的深度
if (left == -1) return -1 //拿到-1,一直返回上去
let right = getHeight(root.right)
if (right == -1) return -1
if (Math.abs(left - right) > 1) return -1 //判断左右节点的相差是不是大于1
else{
res = Math.max(left,right)+1
}
return res
}
};
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
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
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
int l=maxDepth(root.left);
int r=maxDepth(root.right)
return Math.abs(l-r)<=1&&isBalanced(root.left)&&isBalanced(root.right)
}
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
}
共以下 36 个回答
第n次打卡
class Solution {
public boolean isBalanced(TreeNode root) {
return recur(root) != -1;
}
}时间复杂度空间复杂度O(n)
“`C++
class Solution {
public:
int CalDepth(TreeNode* root) {
if(root nullptr) return 0;
int left = CalDepth(root->left);
int right= CalDepth(root->right);
return max(left, right) + 1;
}
bool isBalanced(TreeNode* root) {
if (root nullptr) return true;
int left = CalDepth(root->left);
int right= CalDepth(root->right);
if ((left – right1 || left-right-1 || left right) && isBalanced(root->left) && isBalanced(root->right)) return true;
else return false;
}
};
“`C++
递归的三要素:
1) 函数是干什么的
2) 终结条件
3)找出函数的递归关系
day6第二题
// 时间o(n) 空间o(n)
class Solution {
public:
bool isBalanced(TreeNode* root)
{
return Maxlength(root)>=0;
}
};
平衡二叉树java
代码:
/**
* 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;
}
时空复杂度O(n)
时间复杂度$O(n)$,空间复杂度$O(n)$,其巧妙之处在利用递归和树的后序遍历自底向上,每棵子树都是平衡二叉树则全树必定是平衡二叉树,符合平衡二叉树的定义。
“`c++
//时间复杂度O()
class Solution {
public:
int getDepth(TreeNode* root){
if(!root)
return 0;
int left=getDepth(root->left);
if(left==-1)return -1;
int right=getDepth(root->right);
if(right==-1)return -1;
if((left-right)>1 || (left-right)<-1) return -1; return ((left>right)?left:right)+1;
}
bool isBalanced(TreeNode* root) {
if(!root)
return true;
if(getDepth(root)==-1)return false;
return true;
};
java
class Solution{
public boolean isBalanced(TreeNode root){
}
时间复杂度:O(n2)
空间复杂度:O(n)
// 时间 On,空间O
class Solution {
public:
//先序遍历,判断深度,比较左右子树深度差,若所有子树的深度差都<=1,则此树平衡
private:
//计算二叉树高度
int depth(TreeNode * root){
if(root nullptr) return 0;
return max(depth(root->left),depth(root->right)) +1;
}
};
时间复杂度O(N^2)
从上向下递归,会重复计划,时间复杂度O(N^2)
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root nullptr) return true;
if(calculateDepth (root) -1) return false;
return true;
}
};
时间复杂度O(n) 空间复杂度O(n)
时间复杂度O(n),空间复杂度O(n)
class Solution {
private int calDepth(TreeNode root){//递归计算深度的同时判断左右节点深度差,如果大于1则返回-1而不是深度值。
if(rootnull)
return 0;
int leftDepth = calDepth(root.left);
int rightDepth = calDepth(root.right);
int subtract = leftDepth – rightDepth;
if(leftDepth -1 || rightDepth-1 || subtract < -1 || subtract > 1)
return -1;
else
return Math.max(leftDepth,rightDepth)+1;
}
}
时间复杂度空间复杂度均为On
class Solution {
public:
bool isBalanced(TreeNode* root) {
if (root nullptr) return true;
return abs(deepth(root->left) – deepth(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
private:
int deepth(TreeNode* root){
if (root nullptr) return 0;
return max(deepth(root->left), deepth(root->right)) + 1;
}
};递归判断左右子树的高度,并且左右子树的高度差<=1 ,如果所有子树都平衡,返回true
public boolean isBalanced(TreeNode root) {
if(root null) return true;
return Math.abs(depth(root.left)-depth(root.right))<=1 && isBalanced(root.left)&&isBalanced(root.right);
}
public int depth(TreeNode root){
if(root null) return 0;
return Math.max(depth(root.left),depth(root.right))+1;
}
思路:思路是构造一个获取当前子树的深度的函数 depth(root) ,通过比较某子树的左右子树的深度差 abs(depth(root.left) – depth(root.right)) <= 1 是否成立,来判断某子树是否是二叉平衡树。若所有子树都平衡,则此树平衡。
打卡
https://leetcode.cn/problems/ping-heng-er-cha-shu-lcof/solutions/2672403/pan-duan-avl-by-yukislab-m5de
打卡
思路:定一个函数:
int isBal(Node* root), 然后判断每个节点,如果为空,则返回0;不为空时,递归计算左右子节点的高度,然后计算高度差是否小于等于1,如果是,则返回max(left, right)+1;否则返回-1。如果任一子节点返回-1,则该节点返回-1. -1表示不满足平衡二叉树。“`c++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int isBal(TreeNode *root) {
if (root == nullptr) return 0;
int left = isBal(root->left);
int right = isBal(root->right);
if (left == -1 || right == -1) return -1;
if (abs(left-right) <= 1) return max(left, right) + 1; else return -1; } bool isBalanced(TreeNode* root) { return isBal(root) != -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 isBalanced(self, root: Optional[TreeNode]) -> bool:
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
双 o n
class Solution {
public boolean isBalanced(TreeNode root) {
if(rootnull)
return true;
if(calculateDepth(root)-1)
return false;
return true;
}
public int calculateDepth(TreeNode root) {
if(rootnull)
return 0;
int leftdepth=calculateDepth(root.left);
if(leftdepth-1)
return -1;
int rightdepth=calculateDepth(root.right);
if(rightdepth-1)
return -1;
if(Math.abs(leftdepth-rightdepth)>1)
return -1;
return 1+Math.max(leftdepth,rightdepth);
}
}
利用树的深度判断平衡二叉树,-1标识非平衡二叉树
法一:暴力版 重复计算 时间复杂度O(nlogn),空间复杂度O(logn)
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root nullptr) return true;
return abs(calculateDepth(root->left) – calculateDepth(root->right)) <= 1 && isBalanced(root->left) &&
isBalanced(root->right);
}
};
法二 后序遍历计算
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode root) {
return dfs(root) != -1;
}
};
时间复杂度O(n),空间复杂度O(LOGN)
时间复杂度O(n^2)
空间复杂度O(n)
“`C++
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root == nullptr)
{
return true;
}
};
“`
时间 nlogn 空间 On
时间 nlogn 空间 On