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的更多相关文章

  1. PAT A1138 Postorder Traversal (25 分)——大树的遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  2. PAT_A1138#Postorder Traversal

    Source: PAT A1138 Postorder Traversal (25 分) Description: Suppose that all the keys in a binary tree ...

  3. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  4. [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 ...

  5. 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 ...

  6. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  7. 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 ...

  8. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  9. Leetcode Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

随机推荐

  1. windos安装maven

    1.下载好maven压缩包,并解压到相应位置,本次安装在D: 2.配置环境变量 MAVEN_HOME=D:\apache-maven-3.0.5 path=%MAVEN_HOME% 3.生成maven ...

  2. 排查 Maxwell can not find database 并且使用 MySQL binlog 解决相关问题

    目前我们在使用 Maxwell 在读线上机器的 binlog 同步我们的离线数据库. 这次错误定位上,首先线要确定问题是发生在生产者 还是队列 还是消费者.经过查看各机器上任务的运行日志,定位到了问题 ...

  3. DAY07、字符编码和文件操作

    一.字符编码 1.什么是字符编码? 人类能识别的是字符等高级标识符,电脑只能识别0,1组成的标识符,要完成人与机器之间的信息交流,              一定需要一个媒介,进行两种标识符的转化(两 ...

  4. WEB测试重点--(转载)

    1.功能测试: 所实现的功能是否和需求一致: js错误 页面链接错误-空链接.死链接.错误链接 按钮无效 未实现功能 报错提示信息不准确或不友好 数据库访问错误 sql注入 文档上传下载问题 -未实现 ...

  5. Spring Boot 构建电商基础秒杀项目 (十二) 总结 (完结)

    SpringBoot构建电商基础秒杀项目 学习笔记 系统架构 存在问题 如何发现容量问题 如何使得系统水平扩展 查询效率低下 活动开始前页面被疯狂刷新 库存行锁问题 下单操作步骤多,缓慢 浪涌流量如何 ...

  6. KKT条件

    kkt条件背下来容易.理解上还有问题 主要是lambda≥0和lambda*f(x)=0这两个条件懵逼. 下面说明一下为什么 参考:https://blog.csdn.net/newthinker_w ...

  7. 检索 COM 类工厂中 CLSID 为 {91493441-5A91-11CF-8700-00AA0060263B} 的组件失败

    Symptoms When no user is interactively logged on to the server console, if you try to start a COM+ a ...

  8. MobX基础 ----- 类的静态属性和装饰器

    当我们使用MobX的时候,首先要声明一个store, 用来保存状态,它的最基本的语法 如下: class Todo { @observable title = ""; @obser ...

  9. 前端使用Javascrip实现图片轮播

    Javascript实现网页图片自动轮播 1.创建一个img标签 设置默认图片,以及图片的高度和宽度,为了大家方便,我将CSS样式和JS语句都写在一个html文件中,演示用的图片来自小明官网:'htt ...

  10. .net core 2.0 webuploader上传图片

    引入文件 <link href="~/Scripts/webuploader-0.1.5/webuploader.css" rel="stylesheet" ...