码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 1119 Pre- and Post-order Traversals


    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

    Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

    Sample Input 1:

    1. 7
    2. 1 2 3 4 6 7 5
    3. 2 6 7 4 5 3 1

    Sample Output 1:

    1. Yes
    2. 2 1 6 4 7 3 5

    Sample Input 2:

    1. 4
    2. 1 2 3 4
    3. 2 4 3 1

    Sample Output 2:

    1. No
    2. 2 1 3 4
    1. #include
    2. using namespace std;
    3. int n, pre[50], post[50], in[50], cnt;
    4. bool flag;
    5. struct node {
    6. int left, right;
    7. } tree[50];
    8. int inorder(int prel, int prer, int postl, int postr) {
    9. if (prel == prer) {
    10. return pre[prel];
    11. }
    12. if (pre[prel + 1] == post[postr - 1]) { //不唯一的情况
    13. flag = 1;
    14. tree[pre[prel]].left = inorder(prel + 1, prer, postl, postr - 1);
    15. } else { //唯一的情况
    16. int t = postl;
    17. while (post[t] != pre[prel + 1]) { //寻找后序遍历中左子树的根
    18. t++;
    19. }
    20. tree[pre[prel]].left = inorder(prel + 1, prel + t - postl + 1, postl, t); //左子树
    21. tree[pre[prel]].right = inorder(prel + t - postl + 2, prer, t + 1, postr - 1); //右子树
    22. }
    23. return pre[prel];
    24. }
    25. void dfs(int x) {
    26. if (tree[x].left != 0) {
    27. dfs(tree[x].left);
    28. }
    29. in[cnt++] = x;
    30. if (tree[x].right != 0) {
    31. dfs(tree[x].right);
    32. }
    33. }
    34. int main() {
    35. cin >> n;
    36. for (int i = 0; i < n; i++) {
    37. cin >> pre[i];
    38. }
    39. for (int i = 0; i < n; i++) {
    40. cin >> post[i];
    41. }
    42. inorder(0, n - 1, 0, n - 1);
    43. dfs(pre[0]);
    44. if (flag) {
    45. cout << "No" << endl;
    46. } else {
    47. cout << "Yes" << endl;
    48. }
    49. for (int i = 0; i < cnt; i++) {
    50. cout << in[i];
    51. if (i != cnt - 1) {
    52. cout << ' ';
    53. }
    54. }
    55. cout << endl;
    56. return 0;
    57. }

     参考博客:【后序遍历】+【先序遍历】与【中序遍历】的关系 【PAT】1119 Pre- and Post-order Traversals_Offer.harvester的博客-CSDN博客_后序序列和中序序列的关系

  • 相关阅读:
    【分享】“微伴助手“在集简云平台集成应用的常见问题与解决方案
    SpringBoot SpringBoot 开发实用篇 4 数据层解决方案 4.4 Redis 下载安装与基本使用
    2022年11月vscode调试CPP代码
    [Redis]Redis客户端
    LeetCode笔记:Weekly Contest 318
    2022-9-3 22点 程序爱生活 纳指这波下跌需要缓口气,但是后面更加猛烈,恒指可能有反弹, 但会继续被裹挟下跌,创出新低
    NeRF-VAE:将场景看作一个分布【ICML‘2021】
    curl使用
    分类预测 | MATLAB实现SSA-CNN-BiGRU麻雀算法优化卷积双向门控循环单元数据分类预测
    Day694.Tomcat如何支持异步Servlet -深入拆解 Tomcat & Jetty
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127611133
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号