Skip to content

Commit

Permalink
Create 093. 平衡二叉树.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
gh877916059 authored Sep 8, 2017
1 parent 1f3744d commit 3ae4c5e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 093. 平衡二叉树.cpp
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;
}
};

0 comments on commit 3ae4c5e

Please sign in to comment.