Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >PAT(甲级)1138.Postorder Traversal(25)

PAT(甲级)1138.Postorder Traversal(25)

作者头像
lexingsen
发布于 2022-02-25 00:06:49
发布于 2022-02-25 00:06:49
25100
代码可运行
举报
文章被收录于专栏:乐行僧的博客乐行僧的博客
运行总次数:0
代码可运行

PAT 1138.Postorder Traversal(25) 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. 输入格式: 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.

输出格式: For each test case, print in one line the first number of the postorder traversal sequence of the corresponding binary tree.

输入样例:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
7
1 2 3 4 5 6 7
2 3 1 5 4 7 6

输出样例:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
3

题目分析:模板题,根据中序遍历和前序遍历求解后序遍历序列的第一个元素。

注意:应该牢牢掌握以下三种二叉树的重构。 1.前序+中序 2.中序+后序 3.中序+层序

AC代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <iostream>
using namespace std;

const int maxn = 50010;
int mid[maxn] = {0};
int pre[maxn] = {0};
int post[maxn] = {0};
int n, idx=1;

struct node{
    int data;
    node* lchild;
    node* rchild;
};

node* build(int preL, int preR, int inL, int inR){
    if(preL > preR)return NULL;
    node* root = new node;
    root->data = pre[preL];
    int k;
    for(k = inL; k<=inR; ++k){
        if(mid[k] == pre[preL])
            break;
    }
    int numLeft = k - inL;
    root->lchild = build(preL+1, preL+numLeft, inL, k-1);
    root->rchild = build(preL+numLeft+1, preR, k+1, inR);
    return root;
}

void postorder(node* root){
    if(root){
        postorder(root->lchild);
        postorder(root->rchild);
        post[idx ++] = root->data;
    }
}
int main(){
    scanf("%d", &n);
    for(int i=1; i<=n; ++i){scanf("%d", &pre[i]);}
    for(int i=1; i<=n; ++i){scanf("%d", &mid[i]);}
    node* root = build(1, n, 1, n);
    postorder(root);
    cout<<post[1]<<endl;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
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 duplicates do not exist in the tree. For example, given
全栈程序员站长
2022/08/04
1780
PAT(甲级)1143. Lowest Common Ancestor(30)
PAT 1143. Lowest Common Ancestor(30) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has the following properties: 1.The left subtree of a node contains only nodes with keys less than the node’s key. 2.The right subtree of a node contains only nodes with keys greater than or equal to the node’s key. 3.Both the left and right subtrees must also be binary search trees. Given any two nodes in a BST, you are supposed to find their LCA. 输入格式: Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
lexingsen
2022/02/25
2170
Binary Tree Postorder Traversal — LeetCode
大家好,又见面了,我是你们的朋友全栈君。 原题链接: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 跟 Binary Tree Inorder Traversal 以及 Binary Tree Preorder Traversal 一样,二叉树的后序遍历我们还是介绍三种方法,第一种是递归,第二种是迭代方法,第三种是用线索二叉树。 递归还是那么简单,算法的时间复杂度是O(n), 而空间复杂度则是递归栈的大小,即O(logn)。代码如下:
全栈程序员站长
2022/11/17
2460
PAT(甲级)1051.LCA in a Binary Tree(30)
PAT 1051.LCA in a Binary Tree(30) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. Given any two nodes in a binary tree, you are supposed to find their LCA.
lexingsen
2022/02/25
2220
PAT(甲级)1051.LCA in a Binary Tree(30)
【PAT甲级】Is It a Binary Search Tree
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
喜欢ctrl的cxk
2019/11/08
3360
PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are suppo
ShenduCC
2018/04/27
9600
hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3475    Accepted Submission(s): 1555 Problem Description A binary tree is a finite set of vertices that is either empty or
Gxjun
2018/03/26
8520
hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals(二叉树) - HDU 1710
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.
ACM算法日常
2018/08/07
3980
Binary Tree Traversals(二叉树) - HDU 1710
03-树3 Tree Traversals Again
这一题,需要清楚非递归遍历二叉树的知识,你是否和我一样又回头预习了复习了这个知识呢
废江_小江
2022/09/05
2350
03-树3 Tree Traversals Again
Leetcode 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 duplicates do not exist in the tree. 中序和后序还原二叉树。 直接在上一题的代码上做一点小修改就行了。 /** * Definition for a binary tree node. * struct TreeNode { * int val;
triplebee
2018/01/12
6400
PAT 1020 Tree Traversals (25分) 解读
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
vivi
2020/07/14
5230
二叉排序树
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
喜欢ctrl的cxk
2019/11/08
4690
[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/ 难度:Medium 题目:106. Construct Binary Tree from Inorder and Postorder Traversal
尾尾部落
2018/09/04
5200
「二叉树进阶题解:构建、遍历与结构转化全解析」
可以看见,唯一特殊的就是左子树,当右子树存在的时候左子树不存在的时候,我们需要用()代表空,但是没有左子树,又没有右子树的时候,我们不需要做任何处理。
用户11305458
2024/11/21
1240
「二叉树进阶题解:构建、遍历与结构转化全解析」
PAT甲级题目
PAT甲级的题目有关于树的题目,1053,1086,1090,1102,1106,1115,1119,1038,1110,1020,1043
西红柿炒鸡蛋
2020/03/20
5400
PAT甲级题目
数据结构算法整理-05-二叉树
广义表表示 :L = (A (B (C, D), E ( , F) ) ) 可以得出
devi
2021/08/18
2840
Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree.
卡尔曼和玻尔兹曼谁曼
2019/01/22
4660
数据结构之树
对于树中的每个节点X,它的左子树中所有的关键字值小于X的关键字,而它的右子树中所有的关键字值大于X的关键字值。
心跳包
2020/08/31
2960
数据结构之树
03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历
03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered
llhthinker
2018/01/24
1.3K0
二叉树的数据结构与C++实例
二叉树是一种每个结点至多只有两个子树(即二叉树的每个结点的度不大于2),并且二叉树的子树有左右之分,其次序不能任意颠倒。
里克贝斯
2021/05/21
4950
二叉树的数据结构与C++实例
相关推荐
106. Construct Binary Tree from Inorder and Postorder Traversal「建议收藏」
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档