Skip to content

Commit

Permalink
平衡二叉树
Browse files Browse the repository at this point in the history
使用了abs
  • Loading branch information
DreamOfFootPrints committed Aug 14, 2016
1 parent 770ad96 commit df4ed53
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 9/IsBalanced_Solution.h
@@ -0,0 +1,31 @@
#include<queue>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}

};


int _Hight(TreeNode* pRoot)
{
if (pRoot == NULL) return 0;
int leftDepth = _Hight(pRoot->left);
int rightDepth = _Hight(pRoot->right);

return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}

bool IsBalanced_Solution(TreeNode* pRoot)
{
if (pRoot == NULL)
return true;

int bf = _Hight(pRoot->left) - _Hight(pRoot->right);
return abs(bf) < 2 && IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);

}

0 comments on commit df4ed53

Please sign in to comment.