Skip to content

Commit d21238a

Browse files
committed
Balanced Binary Tree
1 parent ce6beed commit d21238a

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
+ [100 Same Tree](algorithms/SameTree)
3131
+ [101 Symmetric Tree](algorithms/SymmetricTree)
3232
+ [104 Maximum Depth of Binary Tree](algorithms/MaximumDepthofBinaryTree)
33+
+ [110 Balanced Binary Tree](algorithms/BalancedBinaryTree)
3334
+ [111 Minimum Depth of Binary Tree](algorithms/MinimumDepthofBinaryTree)
3435
+ [112 Path Sum(前序遍历)](algorithms/PathSum)
3536
+ [113 Path Sum II(前序遍历)](algorithms/PathSum2)

algorithms/BalancedBinaryTree/'

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <cstdlib>
2+
using namespace std;
3+
struct TreeNode {
4+
int val;
5+
TreeNode *left;
6+
TreeNode *right;
7+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
};
9+
class Solution {
10+
public:
11+
bool isBalanced(TreeNode *root) {
12+
if (root == NULL)
13+
return true;
14+
if (root->left && root->right)
15+
return isBalanced(root->left) && isBalanced(root->right);
16+
if (root->left) {
17+
return root->left->left && !root->left->right;
18+
if (root->left->left || root->left->right)
19+
return false;
20+
else
21+
return true;
22+
}
23+
}
24+
25+
};
26+
int main(int argc, char **argv)
27+
{
28+
return 0;
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Balanced Binary Tree
2+
3+
Given a binary tree, determine if it is height-balanced.
4+
5+
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
6+
7+
## Solution
8+
9+
分别求左右子树的高度,如果高度之差大于1,则不是平衡树
10+
11+
而如果子树不是平衡树,结果也一定不是平衡树,因此再求左右子树的同时,顺便判断是否平衡树,能够提高速度
12+
13+
## Code
14+
```cpp
15+
bool isBalanced(TreeNode *root) {
16+
bool unbalenced = false;
17+
getHeight(unbalenced, root);
18+
return !unbalenced;
19+
}
20+
int getHeight(bool &unbalenced, TreeNode *root) {
21+
if (root == NULL || unbalenced)
22+
return -1;
23+
int left = getHeight(unbalenced, root->left);
24+
int right = getHeight(unbalenced, root->right);
25+
if (abs(left, right) > 1) {
26+
unbalenced = true;
27+
}
28+
return 1 + (left > right ? left : right);
29+
}
30+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <cstdlib>
2+
using namespace std;
3+
struct TreeNode {
4+
int val;
5+
TreeNode *left;
6+
TreeNode *right;
7+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
};
9+
static inline int abs(int a, int b)
10+
{
11+
return a > b ? a - b : b - a;
12+
}
13+
class Solution {
14+
public:
15+
bool isBalanced(TreeNode *root) {
16+
bool unbalenced = false;
17+
getHeight(unbalenced, root);
18+
return !unbalenced;
19+
}
20+
int getHeight(bool &unbalenced, TreeNode *root) {
21+
if (root == NULL || unbalenced)
22+
return -1;
23+
int left = getHeight(unbalenced, root->left);
24+
int right = getHeight(unbalenced, root->right);
25+
if (abs(left, right) > 1) {
26+
unbalenced = true;
27+
}
28+
return 1 + (left > right ? left : right);
29+
}
30+
31+
};
32+
int main(int argc, char **argv)
33+
{
34+
return 0;
35+
}

0 commit comments

Comments
 (0)