sicily_1935_重建二叉树解题报告

传送门:http://soj.sysu.edu.cn/1935

  • 问题已知:一棵树先序遍历序列和中序遍历序列
  • 思路:
    • 将这个树分成三部分:根,左子树,右子树。
    • 先把根插入树中,再左子树和右子树进行递归,直到所有元素都已经插入到树中即可。
// Copyright (c) Junjie_Huang@SYSU(SNO:13331087). All Rights Reserved.
// 1000_sicily_1935.cpp: http://soj.sysu.edu.cn/1935
#include 
#include 
#include 
#include 
#include 

#define MAX_SIZE 1000
#define STRING_SIZE 50

using std::cin;
using std::string;
using std::queue;

// Pre:
// @|tree[]|: the array needs to traverse
// @|pre|: the string printed by traversing a tree in pre-order
// @|mid|: the string printed by traversing a tree in mid-order
// @|index|: the index of parent node in the |tree[]|.
// Post: None
// Usage: finds the root node of a tree and sperates the |mid| with root so we
// can point out the left sub-tree and the right one. Recurses the function
// until all the elements are insert to the |tree|
void rebuild(char tree[], string pre, string mid, int index = 0) {
  // records the left child |left| and the right child |right|
  // of the parent node
  int left = 2 * index + 1, right = 2 * index + 2;
  if (pre.length()) {  // Recursions end when all the elements are inserted.
    tree[index] = pre[0];  // insert the data of the parent node

    // finds the position of root node from |mid|
    int temp = mid.find(pre[0]);

    // Recursions. Seperates the tree to its left sub-tree and the right one.
    // and the |left| and |right| will be the new parent nodes.
    rebuild(tree, pre.substr(1, temp), mid.substr(0, temp), left);
    rebuild(tree, pre.substr(temp + 1), mid.substr(temp + 1), right);
  }
}

// Pre:
// @|tree[]|: the binary tree we want to traverse with BFS. Here because
// we use an array to store the data, so we just need to traverse the array
// by the index.
// Post: Node
void BFS_visit(char tree[]) {
  for (int i = 0; i < MAX_SIZE; i++) {
    if (tree[i]) printf("%c", tree[i]);
  }
  printf("\n");
}

int main() {
  int n = 0;
  char tree[MAX_SIZE + 10];

  for (scanf("%d", &n); n; n--) {
    memset(tree, 0, sizeof(tree));
    string pre_order, mid_order;

    cin >> pre_order >> mid_order;
    rebuild(tree, pre_order, mid_order);

    BFS_visit(tree);
  }

  return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 4,422评论 1 31
  • 给定一个前序和中序变量的结果,写一个算法重建这棵树:前序: a b d c e f中序: d b a e c f...
    HangChen阅读 527评论 0 3
  • 这几天开学,学校还在上课,最近也是在找工作,很多天都没有更新文章,现在补一篇二叉树的文章。 最近校招公司的笔试陆续...
    zero_sr阅读 3,933评论 0 5
  • 一直以来,我都很少使用也避免使用到树和图,总觉得它们神秘而又复杂,但是树在一些运算和查找中也不可避免的要使用到,那...
    24K男阅读 6,725评论 5 14
  • 有人逼我说出他和虫子的区别。 ——— 十月,曝光默片的胶卷! 无声的把戏在人群中定格 放映员举起双手矢口否认 烈日...
    子健阅读 322评论 0 3