zoukankan      html  css  js  c++  java
  • 数据结构之二叉树基础二

    #include <iostream>
    #include <vector>
    #include <cstring>
    #include <stack>
    #include <cmath>
    using namespace std;
    
    struct BTNode
    {
        int val;
        BTNode *lc;
        BTNode *rc;
    };
    typedef struct BTNode;
    
    
    int depth(BTNode* root)
    {
        if(root==NULL)
        {
            return 0;
        }
        int n1=depth(root->lc);
        int n2=depth(root->rc);
        return n1>n2?1+n1:1+n2;
    }
    
    
    //平衡二叉树的判断
    bool isBanlance(BTNode *root)
    {
        if(root==NULL)
        {
            return true;
        }
        int n1=depth(root->lc);
        int n2=depth(root->rc);
        if(abs(n1-n2)<=1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    //二叉树的子结构判断
    bool CheckIfSubTree(BTNode *root1,BTNode *root2)
    {
        if(root2==NULL)
            return true;
        if(root1==NULL)
            return false;
        if(root1->val==root2->val)
            return CheckIfSubTree(root1->lc,root2->lc) &&CheckIfSubTree(root1->rc,root2->rc);
        else
            return CheckIfSubTree(root1->lc,root2) || CheckIfSubTree(root1->rc,root2);
    }
    
    //判断两个二叉树是不是镜像
    bool CheckIfImage(BTNode *root1,BTNode *root2)
    {
        if(root1==NULL&&root2==NULL)return true;
        if(root1==NULL||root2==NULL)return false;
        return (root1->val==root2->val)&&CheckIfImage(root1->lc,root2->rc)&&CheckIfImage(root1->rc,root2->lc);
    }
    
    
    //判断是不是镜像二叉树,一个二叉树
    bool symmetric(BTNode *root)
    {
        if(root==NULL)return true;
        return CheckIfImage(root->lc,root->rc);
    }
    
    
    //求二叉树的镜像二叉树
    void Mirror(BTNode* root)
    {
        if (root != NULL)
        {
            if (root->lc != NULL || root->rc != NULL)
            {
                BTNode* temp = root->lc;
                root->lc = root->rc;
                root->rc = temp;
            }
            if (root->lc != NULL)
            {
                Mirror(root->lc);
            }
            if (root->rc != NULL)
            {
                Mirror(root->rc);
            }
        }
    
    }
    
    
    char str[1000];
    int j=0;
    //序列化->前序遍历字符串
    char* Serialize(BTNode *root)
    {
        if(root==NULL)str[j++]='#';
        else{
            str[j++]=root->val;
            Serialize(root->lc);
            Serialize(root->rc);
        }
        return str;
    }
    
    int i=0;
    BTNode* creat(char *str)
    {
        char c;
        int n=strlen(str);
        BTNode* p=NULL;
        for(;i<n; i++)
        {
            c=str[i];
            if(c=='#')return NULL;
            p=new BTNode;
            p->val=c;
            p->lc=creat(str);
            p->rc=creat(str);
        }
        return p;
    }
    
    //反序列化->根据前序字符串构造二叉树
    BTNode* Deserialize(char *str)
    {
        int i=0;
        return creat(str);
    }
    
    
    
    int main()
    {
        return 0;
    }
    

  • 相关阅读:
    四个足以警醒你一生的故事!
    人们通常不知道自己想要什么,除非你秀出产品给他们看。
    有些话应该记住
    禅语
    是笑话,是悲凉,是笑后的真实,是真实后的悲凉
    vb6.0分隔文本文件读写的两个例程
    boost Serialization源码分析(1)
    养成好习惯,好气质
    日记(海子)
    关于C#的委托与事件的一个小DEMO
  • 原文地址:https://www.cnblogs.com/jasonhaven/p/7355039.html
Copyright © 2011-2022 走看看