判断是否为同一棵树

非递归:

#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define MAX 10
bool Judgefunc(int N1,int N2,int comp[],int temp[]);
int main()
{
    int N,L,comp[MAX],temp[MAX];
    while (1)
    {
        memset(comp,0,sizeof (comp));
        memset(temp,0,sizeof (temp));
        cin>>N;
        if (N==0)
        {
            break;
        }
        cin>>L;
        for (int i=0;i<N;i++)
        {
            cin>>comp[i];
        }
        while (L--)
        {
            for (int i=0;i<N;i++)
            {
                cin>>temp[i];
            }
            if (Judgefunc(N,N,comp,temp))
            {
                cout<<"YES"<<endl;
            }
            else
            {
                cout<<"NO"<<endl;
            }
        }
    }
    return 0;
}
bool Judgefunc(int N1,int N2,int comp[],int temp[])
{
    if (N1!=N2)//千万不要小看了这一条语句,在很多情况下确实会递归到某一过程这里不相等;
    {
        return false;
    }
    else if (N1==0 && N2==0)
    {
        return true;
    }
    else if (N1==1 && N2==1)
    {
        if (comp[0]==temp[0])
        {
            return true;
        }
        else 
        {
            return false;
        }
    }//递归到最后都是以上三种情况,即这是尾递归,所以列上面情况是作为出口条件的;
    else
    {
        int TreeL1[MAX],TreeR1[MAX],TreeL2[MAX],TreeR2[MAX];
        int l1=0,r1=0,l2=0,r2=0;
        for (int i=1;i<N1;i++)
        {
            if (comp[0]<comp[i])
            {
                TreeR1[r1++]=comp[i];
            }
            else if (comp[0]>comp[i])
            {
                TreeL1[l1++]=comp[i];
            }
        }
        for (int i=1;i<N2;i++)
        {
            if (temp[0]<temp[i])
            {
                TreeR2[r2++]=temp[i];
            }
            else if (temp[0]>temp[i])
            {
                TreeL2[l2++]=temp[i];
            }
        }
        return (Judgefunc(l1,l2,TreeL1,TreeL2)&&Judgefunc(r1,r2,TreeR1,TreeR2));
        //看两个树的左右子树是否分别相等;
    }
}
//我觉得这个算法有一点浪费了,因为每一次比较明明comp上一次就弄好了,但是还要递归一次;

建数:

这个算法有一点问题,以后再改:

/*这个算法的思想是先将本数据建立二叉搜索树,再用要比较的数据到树上搜索,如果
在找点的过程中遇到了未访问的节点且不是自己的那说明这个要比较的数据一定与本数据不同
因为这有一点像广度优先搜索的按层来搜索,在本不在这一层这一点的地方出现了那是有问题了*/
#include <bits/stdc++.h>
using namespace std;
typedef struct TNode *BinTree;
struct TNode
{
    int flag;
    int Data;
    BinTree Left;
    BinTree Right;
};
BinTree CreatTree(int N);
BinTree NewNode(int num);
BinTree Insert(BinTree BST, int num);
int check(BinTree BST, int num);
bool Judge(BinTree BST, int N);
void reFlag(BinTree BST);
void freeTree(BinTree BST);
int main()
{
    int N, L;
    BinTree Head;
    cin >> N;
    while (N)
    {
        cin >> L;
        Head = CreatTree(N);
        for (int i = 0; i < L; i++)
        {
            if (Judge(Head, N))
                cout << "Yes" << endl;
            else
                cout << "No" << endl;
            reFlag(Head);
        }
        freeTree(Head);
        cin >> N;
    }
}
BinTree NewNode(int num)
{
    BinTree Head = (BinTree)malloc(sizeof(struct TNode));
    Head->flag = 0;
    Head->Data = num;
    Head->Left = Head->Right = NULL;
    return Head;
}
BinTree Insert(BinTree BST, int num)
{
    if (!BST)
        NewNode(num);
    else
    {
        if (num > BST->Data)
            BST->Right = Insert(BST->Right, num);
        else if (num < BST->Data)
            BST->Left = Insert(BST->Left, num);
    }
    return BST;
}
BinTree CreatTree(int N)
{
    BinTree Head;
    int num;
    cin >> num;
    Head = NewNode(num);
    for (int i = 1; i < N; i++)
    {
        cin >> num;
        Insert(Head, num);
    }
    return Head;
}
int check(BinTree BST, int num) //其实这里没有必要判断一下是否为NULL,因为x的个数就是节点数N;
{
    /* if (!BST)
        return 0;
    if (!BST->flag)
    {
        if (BST->Data == num)
        {
            BST->flag = 1;
            return 1;
        }
        else
            return 0;
    }
    else
    {
        if (BST->Data > num)
            return check(BST->Left, num);
        else if (BST->Data < num)
            return check(BST->Right, num);
        else
            return 0;
    } */
    if (BST->flag)
    {
        if (BST->Data > num)
            return check(BST->Left, num);
        else if (BST->Data < num)
            return check(BST->Right, num);
        else
            return 0;
    }
    else
    {
        if (num == BST->Data)
        {
            BST->flag = 1;
            return 1;
        }
        else
            return 0;
    }
}
bool Judge(BinTree BST, int N)
{
    int num, flag = 0;
    for (int i = 0; i < N; i++)
    {
        cin >> num;
        if (!flag && !check(BST, num))
            flag = 1;
    }
    if (!flag)
        return true;
    else
        return false;
}
void reFlag(BinTree BST)
{
    if (BST->Left)
        reFlag(BST->Left);
    if (BST->Right)
        reFlag(BST->Right);
    BST->flag = 0;
}
void freeTree(BinTree BST)
{
    if (BST->Left)
        freeTree(BST->Left);
    if (BST->Right)
        freeTree(BST->Right);
    free(BST);
}

 

posted @ 2022-02-09 12:05  次林梦叶  阅读(48)  评论(0)    收藏  举报