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 91c1a93 commit 4fd15c6Copy full SHA for 4fd15c6
110/step5.cpp
@@ -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
21
22
23
+ if (abs(left_height - right_height) > 1) {
24
25
26
27
+ return max(left_height, right_height) + 1;
28
29
30
+};
0 commit comments