判断二叉树是否平衡

应用剑指offer

 1 //判断二叉树是否平衡,后序遍历
 2 bool isBalanced(treeNode* root,int& deep){
 3     if(root==NULL){
 4         deep=0;
 5         return true;
 6     }
 7     //左右子树平衡时,根据高度差判断当前是否平衡
 8     int lDeep,rDeep;
 9     if(isBalanced(root->lChild,lDeep)&&isBalanced(root->rChild,rDeep)){
10         if((lDeep-rDeep)<=1 || (rDeep-lDeep)<=1){
11             deep=(lDeep>rDeep?lDeep:rDeep)+1;
12             return true;
13         }
14     }
15     return false;
16 }

 

posted @ 2014-09-20 09:38  liuzhiminxd  阅读(165)  评论(0编辑  收藏  举报