A1138. Postorder Traversal
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 50,000), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the first number of the postorder traversal sequence of the corresponding binary tree.
Sample Input:
7
1 2 3 4 5 6 7
2 3 1 5 4 7 6
Sample Output:
3
#include<cstdio>
#include<iostream>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data;
}node;
int pre[], in[];
node* create(int preL, int preR, int inL, int inR){
if(preL > preR)
return NULL;
node *root = new node;
root->data = pre[preL];
int mid;
for(int i = inL; i <= inR; i++){
if(in[i] == root->data){
mid = i;
break;
}
}
int len = mid - inL;
root->lchild = create(preL + , preL + len, inL, mid - );
root->rchild = create(preL + len + , preR, mid + , inR);
return root;
}
int N;
int cnt = ;
void postOrder(node* root){
if(root == NULL)
return;
if(cnt != )
return;
postOrder(root->lchild);
postOrder(root->rchild);
if(cnt == ){
printf("%d", root->data);
cnt++;
}
}
int main(){
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d", &pre[i]);
}
for(int i = ; i < N; i++){
scanf("%d", &in[i]);
}
node* root = create(, N - , , N - );
postOrder(root);
cin >> N;
return ;
}
总结:
1、题意:由先序和中序序列建树,再输出后序遍历的第一个节点。
A1138. Postorder Traversal的更多相关文章
- PAT A1138 Postorder Traversal (25 分)——大树的遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- PAT_A1138#Postorder Traversal
Source: PAT A1138 Postorder Traversal (25 分) Description: Suppose that all the keys in a binary tree ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
随机推荐
- mysql数据库,安装 !创建!...详解!
package cn.jiayou; /* 一.mysql? a.MySQL是Web世界中使用最广泛的数据库服务器. SQLite的特点? 1.是轻量级.可嵌入,但不能承受高并发访问,适合桌面和移动应 ...
- 规范化Normalization
一.批规范化 Batch Normalization 转自: http://blog.csdn.net/hjimce/article/details/50866313 https://zhuan ...
- 文件操作 chardet使用
#_*_coding:utf-8_*_ import chardet #f = open(file="兼职白领学生空姐模特护士联系方式.txt", mode="rb&qu ...
- servlet篇 之 servlet的访问
三:servlet的访问 使用web.xml文件中的这个<url-pattern>标签中的映射路径,来访问servlet 6.1 在浏览器的地址栏中,直接输入servlet映射的路径来访问 ...
- CentOS安装、配置Nginx反向代理
添加Nginx存储库 sudo yum install epel-release 安装Nginx sudo yum install nginx 启动Nginx sudo systemctl start ...
- java代码自动下载Spring Boot用户手册
本示例演示Spring Boot 1.5.9.RELEASE版本的用户手册下载 pom.xml <?xml version="1.0" encoding="UTF- ...
- verilog parameter 位宽问题
前言 一直以为parameter 的位宽是无限的,其实不然. 流程: 仿真一下就知道啦: 用处: 精准控制位宽理论上会占用更少的内存,其他好像并没有什么卵用,注意不要越界,我这里系统默认32bit位宽 ...
- shopNC 拓扑图
shopNC :
- mysql 0x80004005 unable to connect to any of the specified mysql hosts
语言:c# 问题:偶尔会出现连不上mysql 报标题的这个错误. 解决方法:把server = localhost 改为 =127.0.0.1 或者静态IP ,按着改暂时没出现了,继续观望!
- 「CodeForces - 50C 」Happy Farm 5 (几何)
BUPT 2017 summer training (16) #2B 题意 有一些二维直角坐标系上的整数坐标的点,找出严格包含这些点的只能八个方向走出来步数最少的路径,输出最少步数. 题解 这题要求严 ...