华中尔科技大机试 二叉树遍历 Easy *两序列构建二叉树

基本思想:

要求用两个序列构建新的二叉树,标准写法,注意下;

关键点:

无;

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
using namespace std;

string s1, s2;

struct node {
    char data;
    node* left;
    node* right;
};

node* charge(int l1, int r1, int l2, int r2) {
    if (l1 > r1)
        return NULL;
    int index = -1;
    for (int i = l2; i <= r2; i++) {
        if (s2[i] == s1[l1])
            index = i;
    }
    node* no = new node;
    no->data = s1[l1];
    no->left = charge(l1+1,l1+index-l2,l2,index-1);
    no->right = charge(l1+index-l2+1,r1,index+1,r2);
    return no;
}

void postorder(node* root) {
    if (root == NULL)
        return;
    postorder(root->left);
    postorder(root->right);
    cout << root->data;
}

int main() {
    while (cin >> s1>> s2) {
        node* root = charge(0, s1.size() - 1, 0, s2.size() - 1);
        postorder(root);
        cout << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/songlinxuan/p/12416985.html

时间: 2024-08-02 22:46:28

华中尔科技大机试 二叉树遍历 Easy *两序列构建二叉树的相关文章

Tree Recovery(由先、中序列构建二叉树)

题目来源: http://poj.org/problem?id=2255 题目描述: Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her cr

根据先序和中序序列构建二叉树

说明: 本次实验利用中序和先序序列,采用递归方式来构建二叉树 . 经过几天的失败和思考,我认为递归构建二叉树的过程中最重要的是递归单元,最麻烦的是递归参数的选择和传递. 简单将算法过程用如下流程图来表示:(本帖所用算法及图片均为原创内容,转贴注明出处) 算法:1.根据先序序列,建立根结点T 2.寻找中序序列的根结点位置,并据此位置计算左子树和右子树的区间 3.判断l_start和r_end是否相等,相等则表示只有一个根结点,设置其左右孩子结点为空并结束这一层:若不相等则继续下面步骤: 4.根据2

通过先序遍历和中序遍历后的序列还原二叉树

当我们有一个 先序遍历序列:1,3,7,9,5,11 中序遍历序列:9,7,3,1,5,11 我们可以很轻松的用笔写出对应的二叉树.但是用代码又该如何实现? 下面我们来简单谈谈基本思想. 首先,先序遍历的顺序是根据 根-左孩子-右孩子 的顺序遍历的,那么我们可以率先确认的是先序遍历序列的第一个数就是根节点,然后中序遍历是根据 左孩子-根-右孩子 的顺序遍历的.我们通过先序遍历确认了根节点,那么我们只需要在中序遍历中找到根节点的位置,然后就可以很好地区分出,那些属于左子树的节点,那些是属于右子树的

已知前序(后序)遍历序列和中序遍历序列构建二叉树(Leetcode相关题目)

1.文字描述: 已知一颗二叉树的前序(后序)遍历序列和中序遍历序列,如何构建这棵二叉树? 以前序为例子: 前序遍历序列:ABCDEF 中序遍历序列:CBDAEF 前序遍历先访问根节点,因此前序遍历序列的第一个字母肯定就是根节点,即A是根节点:然后,由于中序遍历先访问左子树,再访问根节点,最后访问右子树,所以我们找到中序遍历中A的位置,然后A左边的字母就是左子树了,也就是CBD是根节点的左子树:同样的,得到EF为根节点的右子树. 将前序遍历序列分成BCD和EF,分别对左子树和右子树应用同样的方法,

数据结构(二十一)二叉树遍历算法的应用与二叉树的建立

一.顺序存储结构对树这种一对多的关系结构实现起来是比较困难的.但是二叉树是一种特殊的树,由于它的特殊性,使得用顺序存储结构也可以实现. 二.二叉树的顺序存储结构就是用一维数组存储二叉树中的结点,并且结点的存储位置,也就是数组的下标要能体现结点之间的逻辑关系,比如双亲与孩子的关系,左右兄弟的关系等. 三.完全二叉树可以将相应下标的结点存到数组的相应下标的位置上,对于一般的二叉树来说,完全可以将其按完全二叉树编号,只不过,把不存在的结点设置为"null"而已.这显然是对存储空间的浪费,所以

清华大学机试 反向输出 Easy

基本思想: 无: 关键点: 无: #include<iostream> #include<vector> #include<algorithm> #include<string> #include<cmath> #include<set> using namespace std; int main() { string s; while (cin >> s) { for (int i = s.size() - 1; i &

清华大学机试 最大最小值 Easy

基本思想: 无: 关键点: 无: #include<iostream> #include<vector> #include<algorithm> #include<string> #include<cmath> #include<set> #include<map> using namespace std; const int maxn = 10010; int ma[maxn]; int main() { int n;

浙江大学机试 畅通工程 Easy

基本思想: 无: 关键点: 无: #include<iostream> #include<string> #include<vector> #include<map> using namespace std; const int maxn = 1020; int father[maxn]; int m, n; bool ma[maxn][maxn]; void init(int n) { for (int i = 0; i <= n; i++) fat

清华大学机试 特殊乘法 Easy

基本思想: 无: 关键点: 无: #include<iostream> #include<vector> #include<string> using namespace std; int charge(string a, string b) { int cnt=0; for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { cnt += int(a[i] - '0')