Skip to content

Commit 49641f2

Browse files
authored
Merge pull request #13 from colorbox/110
110. Balanced Binary Tree
2 parents 9eaf3a3 + 35ae947 commit 49641f2

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

110/step1.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 高さ平衡の定義を勘違いしておりパスできなかったので解説を見て書く
2+
class Solution {
3+
public:
4+
bool isBalanced(TreeNode* root) {
5+
if (!root) {
6+
return true;
7+
}
8+
int left_height = recursive_max_height(root->left, 0);
9+
int right_height = recursive_max_height(root->right, 0);
10+
11+
return abs(right_height - left_height) <= 1 && isBalanced(root->left) && isBalanced(root->right);
12+
}
13+
14+
int recursive_max_height(TreeNode* root, int current_height) {
15+
if (root == nullptr) {
16+
return current_height - 1;
17+
}
18+
19+
int left_height = recursive_max_height(root->left, current_height + 1);
20+
int right_height = recursive_max_height(root->right, current_height + 1);
21+
22+
return max(left_height, right_height);
23+
}
24+
};

110/step2.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// 引数名を変えて 高さであることを直感的にわかるようにした
2+
class Solution {
3+
public:
4+
bool isBalanced(TreeNode* root) {
5+
if (!root) {
6+
return true;
7+
}
8+
9+
int left_height = height(root->left, 0);
10+
int right_height = height(root->right, 0);
11+
12+
return abs(right_height - left_height) <=1 && isBalanced(root->left) && isBalanced(root->right);
13+
}
14+
15+
int height(TreeNode* root, int base_height) {
16+
if (!root) {
17+
return base_height;
18+
}
19+
20+
int left_height = height(root->left, base_height);
21+
int right_height = height(root->right, base_height);
22+
23+
return max(left_height, right_height) + 1;
24+
}
25+
};

110/step3.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// デフォルト引数を使用
2+
// isBalancedの再帰部分がゴチャごチャしていて読みづらさを感じたので自身のチェックと再帰を分離
3+
class Solution {
4+
public:
5+
bool isBalanced(TreeNode* root) {
6+
if (!root) {
7+
return true;
8+
}
9+
10+
int left_height = height(root->left);
11+
int right_height = height(root->right);
12+
13+
if (abs(right_height - left_height) > 1) {
14+
return false;
15+
}
16+
17+
return isBalanced(root->left) && isBalanced(root->right);
18+
}
19+
20+
int height(TreeNode* root, int base_height = 0) {
21+
if (!root) {
22+
return base_height;
23+
}
24+
25+
int left_height = height(root->left);
26+
int right_height = height(root->right);
27+
28+
return max(left_height, right_height) + 1;
29+
}
30+
};

110/step4.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 繰り返し呼ばれるheightについて、キャッシュで高速化を試みたがむしろ低速化した
2+
class Solution {
3+
public:
4+
bool isBalanced(TreeNode* root) {
5+
if (!root) {
6+
return true;
7+
}
8+
9+
int left_height = height(root->left);
10+
int right_height = height(root->right);
11+
12+
return abs(right_height - left_height) <= 1 && isBalanced(root->left) && isBalanced(root->right);
13+
}
14+
15+
map<TreeNode*, int> height_cache;
16+
int height(TreeNode* root, int base_height = 0) {
17+
if (height_cache.count(root)) {
18+
return height_cache[root];
19+
}
20+
if (!root) {
21+
return base_height;
22+
}
23+
24+
int left_height = height(root->left);
25+
int right_height = height(root->right);
26+
27+
height_cache[root] = max(left_height, right_height)+1;
28+
return height_cache[root];
29+
}
30+
};

110/step5.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// キャッシュを使わずにO(N)となるコード
2+
class Solution {
3+
public:
4+
bool isBalanced(TreeNode* root) {
5+
return GetHeight(root) != -1;
6+
}
7+
8+
int GetHeight(TreeNode* root) {
9+
if (!root) {
10+
return 0;
11+
}
12+
13+
int left_height = GetHeight(root->left);
14+
if (left_height == -1) {
15+
return -1;
16+
}
17+
18+
int right_height = GetHeight(root->right);
19+
if (right_height == -1) {
20+
return -1;
21+
}
22+
23+
if (abs(left_height - right_height) > 1) {
24+
return -1;
25+
}
26+
27+
return max(left_height, right_height) + 1;
28+
}
29+
30+
};

0 commit comments

Comments
 (0)