NYOJ 221 Tree (二叉树)
描述
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 creations:
D
/ \
/ \
B E
/ \ \
/ \ \
A C G
/
/
F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
- 输入
The input will contain one or more test cases. Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.) Input is terminated by end of file. - 输出
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root). - 样例输入
DBACEGF ABCDEFG
BCAD CBAD - 样例输出
ACBFGED
CDAB
分析:
题意其实很简单,就是给定你一棵二叉树的先序和中序序列,然后输出这棵二叉树的后序序列。
我们知道对于任何有n个节点的二叉树,都可以由它的中序序列和先序序列(后序序列)来唯一的确定。
对于先序序列,第一个节点必定为该棵树的根节点,然后在中序序列中找到这个节点,即可将该树一份为二,左边的即为根节点的左子树,右边的为根结点的右子树,然后用同样的方法递归寻找左子树和右子树。
代码:
#include<string>
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct Node
{
char data;
Node * lchild;
Node * rchild;
};
Node *build(string pre,string in)
{
Node * root=NULL;
if(pre.length()>0)
{
root=new Node;
root->data=pre[0];
int index=in.find(pre[0]);///在中序序列中找到当前树的根节点
root->lchild=build(pre.substr(1,index),in.substr(0,index));///递归左子树
root->rchild=build(pre.substr(index+1),in.substr(index+1));///递归右子树
}
return root;
}
void post(Node *root)
{
if(root!=NULL)
{
post(root->lchild);///先访问左子树
post(root->rchild);///在访问右子树
printf("%c",root->data);///最后输出根节点
}
}
int main()
{
string pre,in;
while(cin>>pre>>in)
{
Node* root=build(pre,in);///将先序和中序序列转换为后序序列
post(root);///后序序列递归输出
cout<<endl;
}
return 0;
}
NYOJ 221 Tree (二叉树)的更多相关文章
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 543. Diameter of Binary Tree 二叉树的直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- 【第九周】beta-review阶段贡献分分配
组名: 新蜂 组长: 武志远 组员: 宫成荣 谢孝淼 杨柳 李峤 项目名称: java俄罗斯方块NEO 武志远 武志远 武志远 武志远 武志远 宫成荣 宫成荣 杨柳 宫成荣 宫成荣 李峤 杨柳 李峤 ...
- js滚动异步加载数据的思路
<body> <div style="width:200px; height:1000px; border:1px solid red;" id="to ...
- React.js学习笔记(一):组件协同与mixin
组件协同: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...
- HDU4678_Mine
很有意思,很好的题目. 这样的,一个n*m的扫雷地图,告诉你哪些地方是有雷的.一个人如果点在了空白处,那么与其相邻的(八个方向)的数字以及空白都会递归地显示出来,如果点在数字上面,那么就只会显示这一个 ...
- stm32中使用#pragma pack(非常有用的字节对齐用法说明)
#pragma pack(4) //按4字节对齐,但实际上由于结构体中单个成员的最大占用字节数为2字节,因此实际还是按2字节对齐 typedef struct { char buf[3];//bu ...
- python常用模块collections os random sys
Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...
- 【数据库_Mysql】JAVA-数据库Date格式在前台JSP页面的获取
问题: 数据库保存的为date格式的日期 在前台JSP页面显示的为一串数字1487897 解决办法: 数据库表中字段对应的实体对象属性的get方法上添加一行代码 页面即可正常显示
- 在Linux上编译使用tcmalloc
项目需要使用tcmalloc,比较简单的方法是安装tcmalloc相关包(gpertools)后,将tcmalloc的静态库提取出来,在编译项目内核(执行makefile)时,链接上静态库即可. 这里 ...
- 浅谈Android发展趋势分析
去年11月16.17日,我有幸参加了北京2017安卓技术大会,做了关于车载Android系统的演讲,并主持了诸多大咖参与的圆桌讨论,对Android未来几年的发展趋势进行了一番讨论.来自小米.百度.高 ...
- 【UOJ#79】一般图最大匹配(带花树)
[UOJ#79]一般图最大匹配(带花树) 题面 UOJ 题解 带花树模板题 关于带花树的详细内容 #include<iostream> #include<cstdio> #in ...