码迷,mamicode.com
首页 > 其他好文 > 详细

Balanced Binary Tree

时间:2015-05-17 16:30:19      阅读:170      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!