解法1:递归。
#include <iostream>
#include <algorithm>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int depth(TreeNode* node) {
if (!node) return 0;
return max(depth(node->left), depth(node->right)) + 1;
}
bool isBalanced(TreeNode* root) {
if (!root) return true;
if (isBalanced(root->left) && isBalanced(root->right) && (abs(depth(root->left)-depth(root->right))<=1)) return true;
else return false;
}
int main()
{
TreeNode t1(3);
TreeNode t2(9);
TreeNode t3(20);
TreeNode t4(15);
TreeNode t5(7);
t1.left=&t2;
t1.right=&t3;
t3.left=&t4;
t3.right=&t5;
cout<<isBalanced(&t1)<<endl;
TreeNode ta(1);
TreeNode tb(2);
TreeNode tc(2);
TreeNode td(3);
TreeNode te(3);
TreeNode tf(4);
TreeNode tg(4);
ta.left=&tb;
ta.right=&tc;
tb.left=&td;
tb.right=&te;
td.left=&tf;
td.right=&tg;
cout<<isBalanced(&ta)<<endl;
return 0;
}
重写了一下,看起来清楚点:
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
bool isBalanced(TreeNode * root) {
if (!root) return true;
if (!root->left && !root->right) return true;
if (abs(depth(root->left) - depth(root->right)) > 1) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
private:
int depth(TreeNode * root) {
if (!root) return 0;
if (!root->left && !root->right) return 1;
return max(depth(root->left), depth(root->right)) + 1;
}
};
解法2:用ResultType将bool和depth绑定到一起。
代码如下:
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
struct ResultType {
int depth;
bool isBalanced;
ResultType(int dp = 0, bool iB = true) : depth(dp), isBalanced(iB) {}
};
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
bool isBalanced(TreeNode * root) {
return depth(root).isBalanced;
}
private:
ResultType depth(TreeNode * root) {
if (!root) return ResultType();
if (!root->left && !root->right) return ResultType(true, 1);
ResultType left = depth(root->left);
ResultType right = depth(root->right);
int maxDepth = max(left.depth, right.depth);
if (abs(left.depth - right.depth) <= 1 && left.isBalanced && right.isBalanced) {
return ResultType(maxDepth + 1, true);
} else {
return ResultType(maxDepth + 1, false);
}
}
};
下面这个写法更快。
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
bool isBalanced(TreeNode * root) {
return depth(root).isBalanced;
}
private:
ResultType depth(TreeNode * root) {
if (!root) return ResultType();
ResultType left = depth(root->left);
if (!left.isBalanced) return ResultType(-1, false);
ResultType right = depth(root->right);
if (!right.isBalanced) return ResultType(-1, false);
int maxDepth = max(left.depth, right.depth);
if (abs(left.depth - right.depth) <= 1) {
return ResultType(maxDepth + 1, true);
} else {
return ResultType(maxDepth + 1, false);
}
}
};
注意:下面这种写法不对,因为left和right没有判断是否isBalanced。
ResultType depth(TreeNode * root) {
if (!root) return ResultType();
if (!root->left && !root->right) return ResultType(true, 1);
ResultType left = depth(root->left);
ResultType right = depth(root->right);
int maxDepth = max(left.depth, right.depth);
if (abs(left.depth - right.depth) > 1) {
return ResultType(maxDepth + 1, false);
} else {
return ResultType(maxDepth + 1, true);
}
}
本文介绍了两种不同的方法来判断二叉树是否为平衡二叉树。第一种方法使用递归来计算每个节点的深度,第二种方法则通过自定义的数据结构ResultType结合深度和平衡性标志进行判断。
371

被折叠的 条评论
为什么被折叠?



