Skip to content

Commit 93fac0b

Browse files
Merge pull request #53 from rsarthak/Sarthak
added Binary trees Problem
2 parents 728b3e4 + 401408f commit 93fac0b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Trees/BalancedBinarytrees.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
//LeetCode solution of Balanced Binary Trees Problem
13+
class Solution {
14+
public:
15+
int check(TreeNode* root){
16+
if(root==NULL)return 0;
17+
int lh=check(root->left);
18+
int rh=check(root->right);
19+
if(lh==-1 || rh==-1)return -1;
20+
if(abs(lh-rh)>1)return -1;
21+
return max(lh,rh)+1;
22+
}
23+
24+
bool isBalanced(TreeNode* root) {
25+
return check(root)!=-1;
26+
}
27+
};

0 commit comments

Comments
 (0)