We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f93d313 commit 91c1a93Copy full SHA for 91c1a93
110/step3.cpp
@@ -1,4 +1,5 @@
1
// デフォルト引数を使用
2
+// isBalancedの再帰部分がゴチャごチャしていて読みづらさを感じたので自身のチェックと再帰を分離
3
class Solution {
4
public:
5
bool isBalanced(TreeNode* root) {
@@ -9,7 +10,11 @@ class Solution {
9
10
int left_height = height(root->left);
11
int right_height = height(root->right);
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);
18
}
19
20
int height(TreeNode* root, int base_height = 0) {
0 commit comments