Skip to content

Commit 91c1a93

Browse files
committed
Update
1 parent f93d313 commit 91c1a93

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

110/step3.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// デフォルト引数を使用
2+
// isBalancedの再帰部分がゴチャごチャしていて読みづらさを感じたので自身のチェックと再帰を分離
23
class Solution {
34
public:
45
bool isBalanced(TreeNode* root) {
@@ -9,7 +10,11 @@ class Solution {
910
int left_height = height(root->left);
1011
int right_height = height(root->right);
1112

12-
return abs(right_height - left_height) <= 1 && isBalanced(root->left) && isBalanced(root->right);
13+
if (abs(right_height - left_height) > 1) {
14+
return false;
15+
}
16+
17+
return isBalanced(root->left) && isBalanced(root->right);
1318
}
1419

1520
int height(TreeNode* root, int base_height = 0) {

0 commit comments

Comments
 (0)