水下功夫做透,水上才能顺风顺水。

判断一个二叉树

这道题目主要是利用二叉搜索树的一个性质:
二叉搜索树的中序遍历结果是一个升序的序列。
那么问题转变成:中序遍历 + 验证是不是升序.

//判断一个二叉树是否是查找二叉树
bool
isBST((BTNode* root) { if (root==NULL) { //空树 return true; } if (root->lchild == NULL && root->rchild == NULL) {//单点树 return true; } if (root->lchild!=NULL && root->data < root->lchild->data) { return false; } if (root->rchild!=NULL && root->data > root->rchild->data) { return false; } return isBST(root->lchild) && isBST(root->rchild); }
判断二叉搜索树的另一种方法:中序遍历二叉树,将结果放在数组中,看数组是否单调递增。
 //判断两棵二叉树结构是否相同
 思路:不考虑数据内容,结构相同意味着对应的左子树和对应的右子树都结构相同,使用递归.         
 结束条件:
 1.如果两棵二叉树都为空,返回真
 2.如果两棵二叉树一棵为空,另一棵不为空,返回假。
 3.如果两棵二叉树都不为空,如果对应的左子树和右子树结构相同,就返回真,其他返回假。

bool isStructureSame(BTNode* root1, BTNode* root2)
{
    if (root1 == NULL && root2 == NULL){
          return true;
    } else if (pRoot1 == NULL || pRoot2 == NULL) {
          return false;
    }else{
         bool leftRes =  isStructureSame(root1->lchild,root2->lchild);
         bool rightRes =  isStructureSame(root1->rchild,root2->rchild);
         return (leftRes && rightRes);
    }

}

 

//判断两棵二叉树是否相同
两棵二叉树的结构是一样的且对应的每个结点都有着相同的值。

bool isSame(BTNode* root1, BTNode* root2)
{
    if (root1 == NULL && root2 == NULL){
          return true;
    } else if (root1 == NULL || root2 == NULL) {
          return false;
    }else{
         if(root1->data!=root2->data){//不等于肯定不相等,等于了往下走。
            return false;
         }else{
            bool leftRes =  isSame(root1->lchild,root2->lchild);          
            bool rightRes =  isSame(root1->rchild,root2->rchild);          
            return (leftRes && rightRes);
        }
    }

}   

 

//从当前位置看是否包含
bool isContainFromCurPos(BTNode* root, BTNode* root1) {
    //如果root1已经遍历完了都能对应的上,返回true
    if (root1 == NULL){
        return true;
    }
    //如果root1还没有遍历完,root却遍历完了。返回false
    if (root == NULL){
        return false;
    }
    //如果其中有一个点没有对应上,返回false
    if (root->data != root1->data){  
        return false;
    } 
    //如果根节点对应的上,那么就分别去子节点里面匹配
    bool leftRes = isContainFromCurPos(root->left,root1->left); 
    bool rightRes = isContainFromCurPos(root->right,root1->right);
    return leftRes&&rightRes;
}

 

//判断root1是否是root的子树    
bool isSubTree(BTNode* root, BTNode* root1){
     bool result = false;  
    //只有root和root1都非空时,才去判断,其他情况root2都不是root1的子树
     if(root != NULL && root1 != NULL){
         if(root->data==root1->data){
            result = isSame(root,root1);
         } 
         if(!result){
            result = isSubTree(root->lchild,root1);//递归遍历左子树
         }    
         if(!result){
            result = isSubTree(root1->rchild,root1);//递归遍历右子树
         }                
     }
     return result;
}

 

//判断是否是子结构
bool isSubStruction(BTNode* root, BTNode* root1) {

    bool result = false;
    //只有root和root1都非空时,才去判断,否则直接返回false
    if (root != null && root1 != null) {
        
        if(root->data == root1->data){
            //以这个根节点为起点判断是否包含root1
            result = isContainFromCurPos(root,root1);
        }    
        if (!result) {
            result = isSubStruction(root->lchild,root1);
        }
        if (!result) {
            result = isSubStruction(root->rchild,root1);
        }
    }
    return result;
}

 判断完全二叉树

完全二叉树按照从上到下、从左到右的层次遍历,应满足以下两条要求:
1. 若某节点无左孩子,则一定无右孩子。
2. 若某节点缺左或右孩子,则其所有后继一定无孩子
若不满足上述任何一条,均不为完全二叉树。

 

bool isCompeleteBinaryTree(BTNode* root){
    if(root==NULL){
        return false;
    }
    Queue<BTNode*> queue = new Queue<>();                                                              
    in(queue,root); //根入    
   //一次出一个,出来的时候,把俩儿子(左在列前,右在列后)放进去。
    while(!isEmpty(queue)){                    
        BTNode*tmp = out(queue);                          
        if(tmp->lchild!=NULL&&tmp->rchild!=NULL){//完备结点
            in(queue,tmp->lchild);
            in(queue,tmp->rchild);
        }else if(tmp->lchild==NULL&&tmp->rchild!=NULL){//残缺不符合结点
            return false;
        }else{
            if(tmp->lchild!=NULL&&tmp->rchild==NULL){//残缺可能符合结点
                in(queue,tmp->lchild);
            }
            while(!isEmpty(queue)){
                tmp = out(queue);
                if(tmp->lchild!=NULL&&tmp->rchild!=NULL){
                    return false;
                }
            }
        }                                      
    } 
    return true;
}

 

//判断是否是满二叉树,以及其满二叉树的深度
bool isFull(BTNode* root,int depth){
    if(root==NULL){
        depth = 0;
        return true;
    } 
    int ldepth=depth(root->lchild);
    int rdepth=depth(root->rchild);
    if(isFull(root->lchild)&&isFull(root->rchild)&&ldepth==rdepth){
        depth = ldepth+1;
        return true;
    }
    return false;
}

 

//判断一颗二叉树是否为平衡二叉树
平衡二叉树是指以当前结点为根的左右子树高度不得超过1
bool isBlancedTree(BTNode* root)
{
    //空树是平衡二叉树
    if (root == NULL){
        return true;
    }
    int ldepth=depth(root->lchild);
    int rdepth=depth(root->rchild);
    int gap = ldepth - rdepth

    if (gap > 1 || gap < -1){
         return false;
    }
    return isBlancedTree(root->lchild) && isBlancedTree(root->rchild);
}

 

posted @ 2019-03-14 11:26  北方寒士  阅读(280)  评论(0编辑  收藏  举报