class Solution {
public:
bool isBalanced(TreeNode* root) {
if (root == NULL) {
return true;
}
int left = findDepth(root->left);
int right = findDepth(root->right);
return std::abs(left-right) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
int findDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}
int left = findDepth(root->left);
int right = findDepth(root->right);
return std::max(left, right) + 1;
}
};
共以下 26 个回答
时间复杂度:o(n^2)
空间复杂度:o(n)
int getHeight(TreeNode* node) {
if (node NULL) {
return 0;
}
int leftHeight = getHeight(node->left);
int rightHeight = getHeight(node->right);
if (leftHeight – rightHeight > 1 || leftHeight – rightHeight < -1) //判断左右树是否平衡,如果不平衡就返回0,直接false
{
return 0;
}
return 1 + max(leftHeight, rightHeight); //返回深度
}
bool isBalanced(TreeNode* root) {
return getHeight(root) != 0; //返回判断
}
//时间复杂度:O(n^2),空间复杂度:O(n)
//时间复杂度和空间复杂度为O(n)
class Solution{
public: bool isBalanced(TreeNode *root)
{if(root NULL) return true;
if(MaxDepth(root) -1) return false;
return ture;
}
int MaxDepth(TreeNode *root){
if(root NULL) return NULL;
int left = MaxDepth(root->left);
if(left -1) return -1;
int right = MaxDepth(root->right);
if(right -1) return -1;
if(abs(left-right)-1) return -1;
return max(left,right)+1;
}
};
时间On 空间On
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int height(TreeNode root){
if (root nullptr) return 0;
int leftheight = height(root->left);
int rightheight = height(root->right);
if(leftheight -1 || rightheight -1 || abs(leftheight – rightheight)>1){
return -1;
}
else{
return max(leftheight, rightheight) + 1;
}
}
};
时间复杂度:O(N)
空间复杂度:O(N)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// 自顶向下,先序的思想
// bool isBalanced(TreeNode root) {
// if (root nullptr) return true;
// else {
// return abs(height(root->left) – height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
// }
// }
};
计算深度差,并且保证该节点的子树也是平衡二叉树
时间复杂度:O(n)
空间复杂度:O(n)
class Solution {
public:
int height(TreeNode* root){
if(rootNULL) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
if (leftHeight-1||rightHeight-1||abs(leftHeight-rightHeight)>1) {
return -1;
} else {
return max(leftHeight, rightHeight) + 1;
}
}
bool isBalanced(TreeNode* root) {
return height(root)>=0;
}
};
没有思路,根据答案写的
关于剪枝的思路还有更多练习
”’java
class Solution {
}
先用遞歸方式計算出每個節點的深度, 接著我們只要找出左右深度差小於1的node就返回true, 若沒有則返回false;
也同樣做遞歸求解.
時間複雜度: O(n) 空間複雜度: O(n) 取決於遞歸調用的層數, n為節點總個數
//时间on,空间on,
int height(struct TreeNode* root){
if(root NULL){
return 0;
}
int leftHeight = height(root->left);
int rightHeight = height(root->right);
if(leftHeight -1 || rightHeight -1 || fabs(leftHeight-rightHeight) > 1 ){
return -1;
}
bool isBalanced (struct TreeNode* root){
return height(root) >= 0;
}
时间复杂度O(n^2),空间复杂度O(n)
左子树与右子树高度差绝对值大于1,返回-1,左子树或右子树为-1,该节点返回-1
自顶而下的递归
+ 时间复杂度O(N):所有节点只会遍历一遍,不会出现重复计算
+ 空间复杂度O(N):所用空间只取决于递归调用的层数,最大为节点数N
1、回溯
一个平衡二叉树,它的左子树和右子树都要是平衡二叉树,并且左右子树的深度相差不能超过1。
class Solution {
public boolean isBalanced(TreeNode root) {
if(root null){
return true;
}else{
return isBalanced(root.left) && isBalanced(root.right) && Math.abs(depth(root.left) – depth(root.right)) <= 1;
}
}
public int depth(TreeNode root){
if(root null){
return 0;
}
return Math.max(depth(root.left), depth(root.right)) + 1;
}
}
just traverse with a flag 啊
时间复杂度O(n), 空间复杂度O(n)
// 递归求深度的时候,如果当前节点不平衡(ldeep-rdeep>1),连续向上返回-1
// 时间复杂度O(n), 空间复杂度O(n)
递归
递归