forked from gh877916059/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f3744d
commit 3ae4c5e
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Definition of TreeNode: | ||
* class TreeNode { | ||
* public: | ||
* int val; | ||
* TreeNode *left, *right; | ||
* TreeNode(int val) { | ||
* this->val = val; | ||
* this->left = this->right = NULL; | ||
* } | ||
* } | ||
*/ | ||
class Solution | ||
{ | ||
public: | ||
bool isBalanced(TreeNode *root) | ||
{ | ||
if(root==NULL) | ||
return true; | ||
if(abs(getHeight(root->left)-getHeight(root->right))>1) | ||
return false; | ||
return isBalanced(root->left)&&isBalanced(root->right); | ||
} | ||
int getHeight(TreeNode *root) | ||
{ | ||
if(root==NULL) | ||
return 0; | ||
return max(getHeight(root->left),getHeight(root->right))+1; | ||
} | ||
}; |