递归,求左右是否平衡,再判断高度差
-
- class Solution {
- public:
- bool isBalanced(TreeNode *root) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- int height;
- return help(root,hegiht);
- }
- bool help(TreeeNode *root,int &height){
-
- if(root==NULL)
- {
- height=0;
- return true;
- }
- int lh,rh;
- lh=rh=0;
- if(help(root->left,lh ) ==false) return false;
- if(help(root->right,rh)==false) return false;
- height= lh>rh ? lh+1 : rh+1;
- if(abs(lh-rh)>1 )
- return false;
- else
- return true;
- }
- };
Balanced Binary Tree
原文:http://www.cnblogs.com/qiaozhoulin/p/4509899.html