File tree Expand file tree Collapse file tree 5 files changed +139
-0
lines changed Expand file tree Collapse file tree 5 files changed +139
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments