/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root null){
return true;
}
int right = depth(root.right);
int left = depth(root.left);
return Math.abs(right-left)<=1 &&isBalanced(root.right) &&isBalanced(root.left);
}
public int depth(TreeNode root){
if(root ==null){
return 0;
}
int right = depth(root.right);
int left = depth(root.left);
return Math.max(right,left)+1;
}
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null)return true;
int l=MaxPath(root.left);
int r=MaxPath(root.right);
return Math.abs(l-r)<=1&&isBalanced(root.left)&&isBalanced(root.right);
}
public int MaxPath(TreeNode root){
if(root==null)return 0;
return Math.max(MaxPath(root.left),MaxPath(root.right))+1;
}
}
/**时间复杂度O(n)
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isBalanced(root *TreeNode) bool {
_,b:=Height(root)
return b
}
func Height(root *TreeNode) (int,bool) {
if rootnil{
return 0,true
}
l,t:=Height(root.Left)
if !t {
return 0,false
}
r,b:=Height(root.Right)
if !b {
return 0,false
}
if l-r==0||l-r==1{
return l+1,true
}else if l-r==-1{
return r+1,true
}else{
return 0,false
}
class Solution {
boolean ans = true;
public boolean isBalanced(TreeNode root) {
dfs(root);
return ans;
}
public int dfs(TreeNode root) {
if (!ans || root == null) {
return 0;
}
int left = dfs(root.left);
int right = dfs(root.right);
if (Math.abs(left - right) > 1) {
ans = false;
}
return Math.max(left + 1, right + 1);
}
}
class Solution {
boolean isBalanced = true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return isBalanced;
}
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
int len = Math.abs(left-right);
if(len>1){
isBalanced = false;
}
int maxLen = Math.max(left,right) + 1;
return maxLen;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
return maxDepth(root)==-1 ?false:true;
}
public int maxDepth(TreeNode root){
if(root==null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
if(left==-1|| right==-1|| Math.abs(left-right)>1) return -1;
return Math.max(left,right)+1;
}
}
递归
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
if not root:
return True
left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)
if abs(left_depth - right_depth) > 1:
return False
left = self.isBalanced(root.left)
right = self.isBalanced(root.right)
return left and right
def maxDepth(self, root: TreeNode) -> bool:
if not root:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
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;
}
}
时间复杂度O(n),空间复杂度O(n)
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
if(maxDepth(root) == -1) return false;
return true;
}
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int left = maxDepth(root.left);
if(left == -1) return -1;
int right = maxDepth(root.right);
if(right == -1) return -1;
if(Math.abs(left - right) > 1) return -1;
return Math.max(left, right) + 1;
}
}
共以下 49 个回答
“`java
左右子树都必须是平衡二叉树,从而形成递归条件
class Solution {
public boolean isBalanced(TreeNode root) {
return height(root) >= 0;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root null){
return true;
}
int right = depth(root.right);
int left = depth(root.left);
return Math.abs(right-left)<=1 &&isBalanced(root.right) &&isBalanced(root.left);
}
递归
“`java
要判断一颗二叉树是否为平衡二叉树,我们可以通过递归的方式来解决
首先 判断当前节点是否为空,如果为空,返回true
分别计算当前节点的左子树和右子树的深度,如果他们的深度差超过1,返回false
最后 递归判断当前节点的左子树和右子树是否为平衡二叉树,如果都是平衡二叉树,返回true 否则返回false
Public boolean isBalanced(TreeNode root) {
return noodeHeight(root)! = -1;
}
public int nodeHeight(TreeNode root){
if(root == null) return 0;
int left = nodeHeight(root.left);
if(left == -1){
return -1;
}
int right = nodeHeight(root.right);
if(right == -1){
return -1;
}
return Math.abs(left – right )<2?Math.max(left,right)+1;-1;
}
思路:递归实现的depth函数,需要分别计算左子树、右子数的深度,并且计算左右子树的绝对值
【思路】利用了递归实现的depth函数,再利用递归思路,依次对每一个节点做左右子树高度的check
【复杂度】时间复杂度O(nlogn), 空间复杂度O(1)
后再次进行优化,将两个递归合并,时间复杂度降为O(n),空间复杂度变为O(n)?
C++实现
通过深度搜索,来查看左右子树的深度,然后机型比较查看差值是否大于1,然后再对左右两子树再递归查看
时间复杂度O(n),空间复杂度O(n)
要判断一棵二叉树是否为平衡二叉树,我们可以通过递归的方式来解决。
首先,判断当前节点是否为空,如果为空,则返回True。
然后,分别计算当前节点的左子树和右子树的深度,如果它们的深度差超过1,则返回False。
最后,递归地判断当前节点的左子树和右子树是否为平衡二叉树,如果都是平衡二叉树,则返回True,否则返回False。
递归
/**时间复杂度O(n)
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isBalanced(root *TreeNode) bool {
_,b:=Height(root)
return b
}
func Height(root *TreeNode) (int,bool) {
if rootnil{
return 0,true
}
}
时间复杂度O(n), 空间复杂度O(n)
dfs
时间:O(n)
空间:O(n)
可以
public boolean isBalanced(TreeNode root) {
if(root null) return true;
if(maxDepth(root) -1) return false;
return true;
}
public int maxDepth(TreeNode root){
if(root null) return 0;
int left = maxDepth(root.left);
if(left -1) return -1;
int right = maxDepth(root.right);
if(right -1) return -1;
时间复杂度:o(n)
空间复杂度:o(n)
时间复杂度: O(n)
空间复杂度: O(n)
时间复杂度: O(n), 空间复杂度:O(n)
思路:自底向上,计算左右子树高度差
时间复杂度: O(n), 空间复杂度:O(n)
时间复杂度:O(N)
空间复杂度:O(N)
c++递归:
int getmaxdep(TreeNode* temp)
{
if(tempNULL)return 0;
int leftdep = getmaxdep(temp->left);
if(leftdep-1) return -1;
int rightdep = getmaxdep(temp->right);
if(rightdep-1) return -1;
return abs(leftdep-rightdep)>1?-1:1+max(leftdep,rightdep);
}
bool isBalanced(TreeNode* root) {
class Solution {
bool res = true;
public:
bool isBalanced(TreeNode* root) {
int depth = MaxDepth(root);
return res;
}
int MaxDepth(TreeNode* root){
if(rootnullptr){
return 0;
}
int leftdepth = MaxDepth(root->left);
int rightdepth = MaxDepth(root->right);
if(abs(rightdepth-leftdepth)>1){
res = false;
}
return 1+max(leftdepth,rightdepth);
}
};
时间复杂度O(N)
后序遍历DFS,时间复杂度 O(n)
遍历过程中判断左右子树深度相差
时间复杂度为 O(n)
空间复杂度为 O(n)
自丁向下递归
时间复杂度O(N²)最坏
空间复杂度O(N)
复杂度
时间复杂度: $O(n)$
空间复杂度: $O(n)$
Code
//递归
时间复杂度:O(n)
空间复杂度:O(n)
“`
class Solution {
private:
int recur(TreeNode* root){
if(root==nullptr)return 0;
int l = recur(root->left);
if(l==-1) return -1;
int r = recur(root->right);
if(r==-1) return -1;
return (l-r<=1 && l-r>=-1) ? max(l,r)+1 : -1;
}
public:
bool isBalanced(TreeNode* root) {
return recur(root)!=-1;
}//中文
};
“`
自底向上递归算该节点的深度,且在递归的过程中判断,如果发现下面的节点不满足平衡二叉树的条件就直接返回,剪枝
更好的书写方式是如果本节点及下面的节点不满足平衡二叉树的条件,高度返回-1
时间复杂度 O(n) 空间复杂度 O(h)
时间复杂度 O(n) 空间复杂度O(n)
时间复杂度:O(n)
空间复杂度:O(n)
C++递归实现:
class Solution {
public:
int getDepth(TreeNode* node)
{
if(node NULL) return 0;
int leftHeight = getDepth(node->left);
if(leftHeight -1) return -1;
int rightHeight = getDepth(node->right);
if(rightHeight -1) return -1;
return abs(leftHeight – rightHeight) > 1 ? -1 : 1 + max(leftHeight,rightHeight);
}
bool isBalanced(TreeNode* root)
{
if(root NULL) return true;
return getDepth(root) -1 ? false : true;
}
};
采用自底向上的方法来计算深度,时间复杂度为O(n)。如果采用自顶向下的方式,需要对每棵子树都进行相应的操作,时间复杂度为O(n^2)。
balanced必须为array, 因为这样才能让nested function改变他的val. 如果使用int, nested function treats all outer scope variables as local unless explicitly use nonlocal
时空复杂度:O(n)
1、递归判断平衡二叉树时,可能存在很多的重复计算
2、在深度方法中可以根据深度差值直接返回-1
3、在递归判断平衡二叉树时,可根据深度方法是否返回-1来直接返回false
4、返回值不为-1,则结果是返回true
时间复杂度:O(n)
空间复杂度:O(n)
思路:首先递归求解树的深度,一旦左右子树深度>1,就不是平衡二叉树,优化思想是:如果父节点已经判断出不是平衡节点,那么就不用对子节点继续遍历判断。
时间复杂度O(nlogn),空间复杂度O(n)
时间复杂度O(n),空间复杂度O(n)
时间复杂度O(n),空间复杂度O(n)
时间复杂度O(nlogn),空间复杂度O(n)
跟着帅地的思路,二叉树是小白。
时间复杂度n,空间复杂度为n
思路
代码
class Solution {
// nlogn 空间 On
public boolean isBalanced(TreeNode root) {
if(root null) return true;
if(maxDepth(root) -1) return false;
return true;
}
【时间O(nlogn),空间O(n)】
递归判断每个节点