数据结构之二叉树基础二

#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;
}

原文地址:https://www.cnblogs.com/jasonhaven/p/7355039.html