首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >数据结构实验之二叉树四:(先序中序)还原二叉树 (SDUT 3343)

数据结构实验之二叉树四:(先序中序)还原二叉树 (SDUT 3343)

作者头像
Lokinli
发布于 2023-03-09 09:09:23
发布于 2023-03-09 09:09:23
19000
代码可运行
举报
文章被收录于专栏:以终为始以终为始
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <bits/stdc++.h>
using namespace std;
struct node
{
    char data;
    struct node *lc, *rc;
};
char a[100],b[100];
int n;
struct node *creat(int len, char a[], char b[])
{
    if(len == 0) return NULL;
    int i;
    struct node *root;
    root = new node;
    root -> data = a[0];
    for(i = 0; i < len; i ++)
    {
        if(a[0] == b[i])
        {
            break;
        }
    }
    root -> lc = creat(i, a + 1, b);
    root -> rc = creat(len - i - 1,a + i + 1, b + i + 1);
    return root;
};
int fin(struct node *root)
{
   // cout << "yes" << endl;
    int d1, d2;
    int h = 0;
    if(root)
    {
        d1 = fin(root -> lc);
        d2 = fin(root -> rc);
        h = max(d1+1,d2+ 1);
    }
    return h;
}
int main()
{
        while(~scanf("%d",&n))
        {
        scanf("%s %s", a,b);
        struct node *root;
        root = creat(n,a,b);
        int ans = fin(root);
        printf("%d\n",ans);
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-11-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
数据结构实验之二叉树五:层序遍历 (SDUT 3344)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; }; char s[505]; int num; struct node *creat() { struct node *root; if(s[num ++] == ',') { root = NULL; } else { roo
Lokinli
2023/03/09
1570
数据结构实验之二叉树二:遍历二叉树 SDUT 3341
#include <bits/stdc++.h> using namespace std; struct Tree { char data; struct Tree *right; struct Tree *left; }; char str[55]; int num; struct Tree *creat() { struct Tree * root; if(str[num ++] == ',') { root = NULL;
Lokinli
2023/03/09
1230
数据结构实验之二叉树一:树的同构 (SDUT 3340)
题解:把原本结构体的左右子树的类型定义成 int 型,用来存放这个结点的左右子树的编号,分别建造两棵二叉树,按个比较,如果在第二棵树中没有找到,那么就不用在判断了。
Lokinli
2023/03/09
1810
L2-006 树的遍历 (25 分)
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。
Lokinli
2023/03/09
1930
7-23 还原二叉树 (25分) 【PTA 先序 + 中序 求 高度】
输入首先给出正整数N(≤50),为树中结点总数。下面两行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区别大小写)的字符串。
Lokinli
2023/03/09
3190
数据结构实验之二叉树八:(中序后序)求二叉树的深度(SDUT 2804)
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char data ; struct node *l,*r; }; struct node *creat(char *inorder,char *postorder,int len) { struct node *root; if (len <= 0) return NULL; int i = 0;
Lokinli
2023/03/09
1810
数据结构实验之查找二:平衡二叉树 (SDUT 3374)
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; int h; struct node *lc,*rc; //平衡二叉树 需要一个 h 来记录平衡因子 }; int max(int x ,int y) { if(x > y) return x; else return y; } int fin(struct node *root) //
Lokinli
2023/03/09
2060
L2-011 玩转二叉树 (25 分)
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。
Lokinli
2023/03/09
2960
中序遍历二叉树(栈思想)
完整代码如下,这是我在做了广义表建立二叉树后顺便写的中序遍历二叉树,所以输入是广义表
用户2965768
2018/12/18
5900
7-4 是否同一棵二叉搜索树 (25 分)
给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。
Lokinli
2023/03/09
1870
求二叉树的层次遍历(SDUT 2824)
输入数据有多组,输入T,代表有T组测试数据。每组数据有两个长度小于50的字符串,第一个字符串为前序遍历,第二个为中序遍历。
Lokinli
2023/03/09
1940
二叉树面试题-你已经是棵成熟的二叉树了,要学会自己解题
如果一棵树只有一个节点,那么它的深度是1.如果根节点只有左子树,那深度是其左子树的深度+1,同样的只有右子树的话,深度是其右子树深度+1,如果既有左子树又有右子树,取两个子树的深度最大值+1即可。用递归很容易实现。
唔仄lo咚锵
2022/05/08
3000
二叉树面试题-你已经是棵成熟的二叉树了,要学会自己解题
数据结构实验之链表八:Farey序列(SDUT 3331)
#include <bits/stdc++.h> using namespace std; typedef struct node { int data2; int data1;//mu struct node *next; } ; int main() { int n; struct node *head,*p,*q,*t,*tail; scanf("%d",&n); head = new node; head -> next = NULL;
Lokinli
2023/03/09
1470
数据结构实验之链表二:逆序建立链表(SDUT 2117)
题目链接 #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; int main() { int n; struct node *head,*p; head = new node; head -> next = NULL; scanf("%d",&n); for(int i = 0; i < n; i ++)
Lokinli
2023/03/09
2050
输入广义表建立二叉树(层序输出)
#include <bits/stdc++.h> using namespace std; struct node{ char ch; node *lc,*rc; }; char s[100]; int flag=0; int n; int i=0; node * build_tree() { if(i>=n) return NULL; node *p = new node; p->lc = NULL; p->rc = NULL; p->ch = '0';//初始化 for(;i<n;
用户2965768
2018/12/18
1.1K0
数据结构实验之二叉树三:统计叶子数 SDUT 3342
#include <stdio.h> #include <string.h> struct node { char data; struct node *l,*r; }; struct node *root; char st[51]; int i; int count; struct node *creat() { struct node *root; if(st[i++] == ',') root = NULL; else {
Lokinli
2023/03/09
1250
数据结构实验之栈与队列六:下一较大值(二)(SDUT 3333)
#include <bits/stdc++.h> using namespace std; int a[1000006]; int b[1000006]; int sta[100006]; int main() { int t,n,i,j,top; while(~scanf("%d",&t)) { while(t--) { scanf("%d",&n); for( i = 1; i <= n; i ++)
Lokinli
2023/03/09
1490
数据结构实验之链表四:有序链表的归并(SDUT 2119)
#include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; struct node *creat(int n) { struct node *head,*tail,*p; head=(struct node*)malloc(sizeof(struct node)); head->next=NULL; tail=head;
Lokinli
2023/03/09
1970
二叉树及其三种遍历[通俗易懂]
<3>.若二叉树按照从上到下从左到右依次编号,则若某节点编号为k,则其左右子树根节点编号分别为2k和2k+1;
全栈程序员站长
2022/08/23
1.1K0
二叉树及其三种遍历[通俗易懂]
数据结构实验之链表五:单链表的拆分(SDUT 2120)
#include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; struct node *crea(int n) { int i; struct node *head, *p; head = (struct node *)malloc(sizeof(struct node)); head -> next = NULL; for(i
Lokinli
2023/03/09
2110
推荐阅读
相关推荐
数据结构实验之二叉树五:层序遍历 (SDUT 3344)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档