Skip to content

Commit d1457df

Browse files
authored
Create main.cpp
1 parent 7ef5ab0 commit d1457df

File tree

1 file changed

+21
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/08 - Balanced Tree Check

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution{
2+
public:
3+
int heights(Node* root){
4+
if(root == NULL) return 0;
5+
6+
int left = heights(root -> left);
7+
if(left == -1) return -1;
8+
9+
int right = heights(root -> right);
10+
if(left == -1) return -1;
11+
12+
if(abs(left - right) > 1) return -1;
13+
14+
return max(left, right) + 1;
15+
}
16+
bool isBalanced(Node *root)
17+
{
18+
return heights(root) != -1;
19+
20+
}
21+
};

0 commit comments

Comments
 (0)