PTA 04-树4 是否同一棵二叉搜索树(建立二叉搜索树+遍历判断两棵树是否等价)...

大体思路:

1.建立二叉搜索树(基于链表)

2.判断两棵树是否等价(遍历一遍,vector维护值)

用vector维护是个很笨的方法(主要是我递归没写出来?):用任意一种方式遍历两棵二叉树,记录在两个vector里,如果两vector相等,那么两棵树等价,否则不等价

二叉树:

typedef struct treeNode *tree;
typedef struct treeNode
{
    int val;
    tree left, right;
}node;

生成新节点函数:

void newNode(tree &t, int val)//建立一个新节点
{
    t=new node;
    t->val=val;
    t->left=t->right=NULL;
}

插入节点函数(递归。如果节点为空,new一个新空间;不为空则递归):

void nodeInsert(tree &t, int val)//插入节点。传入待插入节点,返回该节点
{
    if(!t) newNode(t, val);
    else {
        if(val>t->val) nodeInsert(t->right, val);
        else nodeInsert(t->left, val);
    }
}

建树函数:

void treeCreate(tree &t, int n)//建树
{
    int val;cin>>val;//建立根
    newNode(t, val);
    for(int i=1;i<n;++i){
        cin>>val;
        nodeInsert(t, val);
    }
}

 

代码(引用的写法&):

#include <bits/stdc++.h>
using namespace std;
//建立bst(链表实现)+判断是否为同一棵bst

typedef struct treeNode *tree;
typedef struct treeNode
{
    int val;
    tree left, right;
}node;

void newNode(tree &t, int val)//建立一个新节点
{
    t=new node;
    t->val=val;
    t->left=t->right=NULL;
}

void nodeInsert(tree &t, int val)//插入节点。传入待插入节点,返回该节点
{
    if(!t) newNode(t, val);
    else {
        if(val>t->val) nodeInsert(t->right, val);
        else nodeInsert(t->left, val);
    }
}

void treeCreate(tree &t, int n)//建树
{
    int val;cin>>val;//建立根
    newNode(t, val);
    for(int i=1;i<n;++i){
        cin>>val;
        nodeInsert(t, val);
    }
}

void lrd(tree t, vector<int> &v)//lrd后序遍历
{
    if(t){
        lrd(t->left, v);
        lrd(t->right, v);
        v.push_back(t->val);
    }
}

void ldr(tree t)//ldr中序遍历
{
    if(t){
        ldr(t->left);
        cout<<t->val<<endl;
        ldr(t->right);
    }
}

void dlr(tree t)//dlr前序遍历
{
    if(t){
        cout<<t->val<<endl;
        dlr(t->left);
        dlr(t->right);
    }
}

bool check(tree t1, tree t2)//用了个很笨的方法,用vector维护两棵树遍历一遍的值,如果两vector相等,那么两棵树等价,否则不等价
{
    vector<int> v1, v2;
    postTraverse(t1, v1), postTraverse(t2, v2);
    /*for(int i=0;i<v1.size();++i) cout<<v1[i]<<' ';
    cout<<endl;
    for(int i=0;i<v2.size();++i) cout<<v2[i]<<' ';
    cout<<endl;*/
    if(v1.size()!=v2.size()) return 0;
    for(int i=0;i<v1.size();++i)
        if(v1[i]!=v2[i]) return 0;
    return 1;
}

int main()
{
    int n, l;
    tree t;
    while(cin>>n&&n){
        cin>>l;
        treeCreate(t, n);
        while(l--){
            tree tt;
            treeCreate(tt, n);
            if(check(t, tt)) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
    }
    return 0;
}

返回指针的写法(因为返回一个指针的函数写法我还没怎么见识过,特此记录):

#include <bits/stdc++.h>
using namespace std;

typedef struct treeNode *tree;
struct treeNode
{
    int val;
    tree left, right;
};

tree newNode(int val)//建立一个新节点
{
    tree t=new (struct treeNode);
    t->val=val;
    t->left=t->right=NULL;
    return t;
}

tree nodeInsert(tree t, int val)//插入节点。传入待插入节点,返回该节点
{
    if(!t) t=newNode(val);
    else {
        if(val>t->val) t->right=nodeInsert(t->right, val);
        else t->left=nodeInsert(t->left, val);
    }
    return t;
}

tree treeCreate(int n)
{
    tree t;int val;
    cin>>val;t=newNode(val);
    for(int i=1;i<n;++i){
        cin>>val;
        t=nodeInsert(t, val);
    }
    return t;
}

void postTraverse(tree t, vector<int> &v)
{
    if(t){
        postTraverse(t->left, v);
        postTraverse(t->right, v);
        v.push_back(t->val);
    }
}

bool check(tree t1, tree t2)
{
    vector<int> v1, v2;
    postTraverse(t1, v1), postTraverse(t2, v2);
    /*for(int i=0;i<v1.size();++i) cout<<v1[i]<<' ';
    cout<<endl;
    for(int i=0;i<v2.size();++i) cout<<v2[i]<<' ';
    cout<<endl;*/
    if(v1.size()!=v2.size()) return 0;
    for(int i=0;i<v1.size();++i)
        if(v1[i]!=v2[i]) return 0;
    return 1;
}
int main()
{
    int n, l;
    tree t;
    while(cin>>n&&n){
        cin>>l;
        t=treeCreate(n);
        while(l--){
            tree tt=treeCreate(n);
            if(check(t, tt)) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
    }
    return 0;
}

回头尝试用数组实现二叉搜索树,占坑。

 

转载于:https://www.cnblogs.com/ChenyangXu/p/10733262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值