PAT A 1119. Pre- and Post-order Traversals (30)【二叉树遍历】
题目:由前序后序二叉树序列,推中序,判断是否唯一后输出一组中序序列
思路:前序从前向后找,后序从后向前找,观察正反样例可知,前后序树不唯一在于单一子树是否为左右子树。
判断特征:通过查找后序序列中最后一个结点的前一个在先序中的位置,来确定是否可以划分左右孩子,如果不能, 就将其划分为右孩子(或左孩子),递归建树。
中序遍历输出。
#include <iostream>
using namespace std;
const int maxn = 31; int n, index = 0;
int pre[maxn], post[maxn];
bool flag = true; struct Node {
int data;
Node *lchild, *rchild;
} *root; Node *create(int preL, int preR, int postL, int postR)
{
if (preL > preR) return NULL;
Node *node = new Node;
node->data = pre[preL];
node->lchild = NULL;
node->rchild = NULL;
if (preL == preR)
return node;
int k = 0;
for (k = preL + 1; k <= preR; k++)
{
if (pre[k] == post[postR - 1]) break;
}
if (k - preL > 1)
{
node->lchild = create(preL + 1, k - 1, postL, postL + k - preL - 2);
node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
}
else
{
flag = false;
node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
}
return node;
} void inOrder(Node *node)
{
if (node == NULL) return;
inOrder(node->lchild);
if (index < n - 1)
cout << node->data << " ";
else cout << node->data << endl;
index++;
inOrder(node->rchild);
} int main()
{
cin >> n;
for (int i = 0; i < n; ++i) cin >> pre[i];
for (int i = 0; i < n; ++i) cin >> post[i];
root = create(0, n - 1, 0, n - 1);
if (flag) cout << "Yes\n";
else cout << "No\n";
inOrder(root);
return 0;
}
PAT A 1119. Pre- and Post-order Traversals (30)【二叉树遍历】的更多相关文章
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- [二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)
1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct po ...
- Construct a tree from Inorder and Level order traversals
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]
1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...
随机推荐
- js-JavaScript高级程序设计学习笔记19
第22章 高级技巧 1.高级函数 1.安全的类型检测. typeof,instanceof并非完全可靠. 安全的类型检测:使用Object原生的toString()方法. function isArr ...
- Python的方法解析顺序(MRO)[转]
本文转载自: http://hanjianwei.com/2013/07/25/python-mro/ 对于支持继承的编程语言来说,其方法(属性)可能定义在当前类,也可能来自于基类,所以在方法调用时就 ...
- maven的eclise配置
http://blog.csdn.net/guanning0109/article/details/26069277
- Google 地图 API V3 针对移动设备进行开发
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- 开发unity DLL插件
最近开发一款设备的SDK,想着要开发unity版本,怎么做呢?首先想到的就是在外部编写相关的驱动程序然后集成成几个dll作为unity的SDK使用了.So,我就开始了unity外部插件的研究之旅了. ...
- Fast Member
https://github.com/mgravell/fast-member/ http://stackoverflow.com/questions/6158768/c-sharp-reflecti ...
- PHP 图片上传
PHP上传的简单案例: Html文件: <html> <form action="index.php" name="form" method= ...
- .NET导入Excel到SQL数据库
在我们开发各类应用型系统,经常会遇到导入导出Excel,为什么会用到他呢?企业或者单位在从无信息化到信息化的一个转变过程.在没有信息化的企业或单位之前,一般都采用Excel来记录相应的数据,做统计计算 ...
- IOS系统基础知识
在iOS应用中,每个程序得main函数中都调用了UIApplicationMain函数. 1 2 3 4 5 6 int main(int argc, char *argv[]) { @a ...
- http_build_query 的一个问题
当我们使用CURL来post数据的时候,需要设置post的数据 curl_setopt($c, CURLOPT_POSTFIELDS, $post_data); 假如这里的$data是 $data = ...