Skip to content

Commit 854a901

Browse files
authoredSep 16, 2023
110. Balanced Binary Tree
1 parent 2ec135c commit 854a901

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎110. Blanced Binary Tree

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
int height(TreeNode* root){
2+
if(!root)return 0;
3+
// int leftheight = height(root->left);
4+
// int rightheight = height(root->right);
5+
int ans = 1+ max(height(root->left),height(root->right));
6+
return ans;
7+
}
8+
bool isBalanced(TreeNode* root) {
9+
if(!root)return true;
10+
//1 case
11+
// int leftHeight = height(root->left);
12+
// int rightHeight = height(root->right);
13+
int diff = abs(height(root->left)-height(root->right));
14+
15+
//bool ans = (diff <=1);
16+
17+
//Recursion
18+
// int leftans = isBalanced(root->left);
19+
// int rightans = isBalanced(root->right);
20+
21+
if((diff <=1) && isBalanced(root->left) && isBalanced(root->right)){
22+
return true;
23+
}
24+
else{
25+
return false;
26+
}
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.