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 ...
随机推荐
- java.lang.NoClassDefFoundError: org/apache/log4j/Priority的问题解决
在pom 文件中添加 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artif ...
- for 循环增强
package cn.zhou.com; /* * 增强for循环 * * for(int i:arr) * { * System.out.print(i+1+" "); * } ...
- Hibernate 配置文件hibernate.cfg.xml的详细
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml ...
- 为AI提供数据:构建2017数据创新的总结
本周在微软年度大会上,我们正在讨论组织如何依靠开发人员创造突破性的经验.随着大数据,云和人工智能的融合,创新与破坏正在加速,从未见过.数据是这一融合核心的关键战略资产.当结合云的无限计算能力和机器学习 ...
- hdu-2717(基础搜索bfs)
题意:给你n和k,问你n最少花费多少代价能得到k: 有两种变换:1.n++或者n--: 2.n=n*2: 两种代价每次的花费都是1: 思路:一维的bfs,每次入队三个点,一个是n+1,一个是n-1,一 ...
- 训练赛-Building Numbers
题意:首先告诉你,一个数字从1开始有两种变换方式:1.当前数字的值加1 2.当前的数字值乘2: 思路:首先把数组里的数字需要的变换次数算出来,然后用前缀和解决: 代码: #include<ios ...
- Spring 使用介绍(五)—— AOP(一)
一.简单使用:Hello World实例 1.定义目标类 public interface Hello { void sayHello(); } public class HelloImpl impl ...
- Redis——Linux(centos7.x)下Redi和PHP Redis插件安装——【一】
Redis 安装 下载地址:http://redis.io/download,下载最新文档版本. $ wget http://download.redis.io/releases/redis-4.0. ...
- 洛谷P1582 倒水题解
题目 分析 这个题并不难,只是需要仔细思考我们首先可以很轻松的把这个题给疏通一下题意. 1:首先我们最后每个瓶子中装的水一定是一个$2^x$,因为每次都是$2$倍的加,这个应该很好理解. 2:我们要明 ...
- 四种对话框(dialog)的简单使用方法
有普通对话框,单选对话框,复选对话框,进度条的两种实现方法话不多说,直接上代码 activity_main.xml: <?xml version="1.0" encoding ...