首页 > 其他 > 详细

Balanced Binary Tree

时间:2015-05-17 16:28:40      阅读:139      评论:0      收藏:0      [点我收藏+]

递归,求左右是否平衡,再判断高度差

  1.   
  2. class Solution {  
  3. public:  
  4.     bool isBalanced(TreeNode *root) {  
  5.         // Start typing your C/C++ solution below  
  6.         // DO NOT write int main() function  
  7.         int height;  
  8.         return help(root,hegiht);  
  9.     }  
  10.     bool help(TreeeNode *root,int &height){  
  11.           
  12.         if(root==NULL)  
  13.         {  
  14.             height=0;  
  15.             return true;  
  16.         }  
  17.         int lh,rh;  
  18.         lh=rh=0;  
  19.         if(help(root->left,lh ) ==false) return false;  
  20.         if(help(root->right,rh)==false) return false;  
  21.         height= lh>rh ? lh+1 : rh+1;  
  22.         if(abs(lh-rh)>1 )  
  23.             return false;  
  24.         else  
  25.             return true;  
  26.     }  
  27. };  

Balanced Binary Tree

原文:http://www.cnblogs.com/qiaozhoulin/p/4509899.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!