-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy path110. Blanced Binary Tree.cpp
More file actions
27 lines (24 loc) · 818 Bytes
/
110. Blanced Binary Tree.cpp
File metadata and controls
27 lines (24 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int height(TreeNode* root){
if(!root)return 0;
// int leftheight = height(root->left);
// int rightheight = height(root->right);
int ans = 1+ max(height(root->left),height(root->right));
return ans;
}
bool isBalanced(TreeNode* root) {
if(!root)return true;
//1 case
// int leftHeight = height(root->left);
// int rightHeight = height(root->right);
int diff = abs(height(root->left)-height(root->right));
//bool ans = (diff <=1);
//Recursion
// int leftans = isBalanced(root->left);
// int rightans = isBalanced(root->right);
if((diff <=1) && isBalanced(root->left) && isBalanced(root->right)){
return true;
}
else{
return false;
}
}