• 03-树3 Tree Traversals Again


    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 from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

    有序二叉树遍历可以用堆栈以非递归方式实现。例如,假设遍历6节点二叉树(键编号从1到6)时,堆栈操作为:push(1);推(2);推(3);pop();pop();推动(4);pop();pop();推(5);推动(6);pop();pop()。然后,可以从这个操作序列中生成一个唯一的二叉树(如图1所示)。您的任务是给出此树的后序遍历序列。


    Figure 1

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

    每个输入文件包含一个测试用例。对于每种情况,第一行包含正整数N(≤30),这是树中节点的总数(因此节点从1到N编号)。接下来是2N行,每行都以如下格式描述堆栈操作:“Push X”,其中X是被推到堆栈上的节点的索引;或“Pop”表示从堆栈中弹出一个节点。

    Output Specification:

    For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

    对于每个测试用例,在一行中打印相应树的后序遍历序列。保证存在解决方案。所有的数字必须用一个空格隔开,并且行的末尾不能有多余的空格。

    Sample Input:

    1. 6
    2. Push 1
    3. Push 2
    4. Push 3
    5. Pop
    6. Pop
    7. Push 4
    8. Pop
    9. Pop
    10. Push 5
    11. Push 6
    12. Pop
    13. Pop

    Sample Output:

    3 4 2 6 5 1

    思路:

    根据给定push,pop过程我们可以建一棵树,然后后序遍历

    也可以根据push得出先序遍历,pop得出中序遍历,将问题转化为根据先序和中序求后序

    当然,建树的都会了,不建树也可以啦

    代码(建树然后后序遍历):

    1. #include
    2. using namespace std;
    3. typedef pair<int, int> PII;
    4. const int N = 1010;
    5. struct Tree {
    6. int e;
    7. Tree* lchild, * rchild;
    8. };
    9. int i,n;
    10. string s;
    11. bool fg = true;
    12. void Create(Tree*& t,int x) {
    13. t = (Tree*)malloc(sizeof(struct Tree));
    14. t->e = x;
    15. t->lchild = t->rchild = NULL;
    16. }
    17. void BuildTree(Tree*& t){
    18. // if (i == n)return ;
    19. // i++;
    20. cin >> s;
    21. if (s.size() == 4) {
    22. int x;
    23. cin >> x;
    24. Create(t, x);
    25. }
    26. else return;
    27. BuildTree(t->lchild);
    28. BuildTree(t->rchild);
    29. }
    30. void fun(Tree* t) {
    31. if (t == NULL)return;
    32. fun(t->lchild);
    33. fun(t->rchild);
    34. if (fg) {
    35. fg = false;
    36. cout << t->e ;
    37. }else cout <<" "<< t->e;
    38. }
    39. int main()
    40. {
    41. cin >> n;
    42. Tree* t;
    43. BuildTree(t);
    44. fun(t);
    45. return 0;
    46. }

    代码(用先序序列和中序序列建树):

    1. #include
    2. using namespace std;
    3. typedef pair<int, int> PII;
    4. const int N = 35;
    5. struct Tree {
    6. int e;
    7. Tree* lchild, * rchild;
    8. };
    9. int i, n;
    10. string s;
    11. vector<int> pre, in;
    12. stack<int> stk;
    13. bool fg = true;
    14. void Create(Tree*& t, int x) {
    15. t = (Tree*)malloc(sizeof(struct Tree));
    16. t->e = x;
    17. t->lchild = t->rchild = NULL;
    18. }
    19. void BuildTree(Tree*& t, int root, int l, int r) {
    20. if (l > r)return;
    21. Create(t, pre[root]);
    22. int k = 0, i = l;
    23. for (; i <= r; i++)if (pre[root] == in[i])break;
    24. BuildTree(t->lchild, root + 1, l, i - 1);
    25. BuildTree(t->rchild, root + i - l + 1, i + 1, r);
    26. }
    27. void fun(Tree* t) {
    28. if (t == NULL)return;
    29. fun(t->lchild);
    30. fun(t->rchild);
    31. if (fg) {
    32. fg = false;
    33. cout << t->e;
    34. }
    35. else cout << " " << t->e;
    36. }
    37. int main()
    38. {
    39. cin >> n;
    40. for (int i = 0; i < 2 * n; i++) {
    41. cin >> s;
    42. if (s.size() == 4) {
    43. int x;
    44. cin >> x;
    45. pre.push_back(x);
    46. stk.push(x);
    47. }
    48. else {
    49. in.push_back(stk.top());
    50. stk.pop();
    51. }
    52. }
    53. Tree* t;
    54. BuildTree(t, 0, 0, n - 1);
    55. fun(t);
    56. return 0;
    57. }

    代码(当然,不建树也可以啦):

    1. #include
    2. using namespace std;
    3. typedef pair<int, int> PII;
    4. const int N = 35;
    5. int i, n;
    6. string s;
    7. vector<int> pre, in;
    8. stack<int> stk;
    9. bool fg = true;
    10. vector<int> ans;
    11. void Create_post(int root, int l, int r) {
    12. if (l > r)return;
    13. int res = root;
    14. int k = 0, i = l;
    15. for (; i <= r; i++)if (pre[root] == in[i])break;
    16. Create_post(root + 1, l, i - 1);
    17. Create_post(root + i - l + 1, i + 1, r);
    18. ans.push_back(pre[res]);
    19. }
    20. int main()
    21. {
    22. cin >> n;
    23. for (int i = 0; i < 2 * n; i++) {
    24. cin >> s;
    25. if (s.size() == 4) {
    26. int x;
    27. cin >> x;
    28. pre.push_back(x);
    29. stk.push(x);
    30. }
    31. else {
    32. in.push_back(stk.top());
    33. stk.pop();
    34. }
    35. }
    36. Create_post(0, 0, n - 1);
    37. for (int i = 0; i < ans.size(); i++) {
    38. if (fg) {
    39. fg = false;
    40. cout << ans[i];
    41. }
    42. else cout << " " << ans[i];
    43. }
    44. return 0;
    45. }

  • 相关阅读:
    谷歌云:下一代开发者和企业解决方案的强力竞争者
    vue项目配置MongoDB的增删改查操作
    离线生成双语字幕,一键生成中英双语字幕,基于AI大模型,ModelScope
    SQL使用场景解决一对多查询、分页、复杂排名等问题之ROW_NUMBER、DENSE_RANK、RANK用法
    自己动手写一个分库分表中间件(九)兼容性处理之事务之 Spring 怎么看是一个事务
    盘点 Udemy 上最受欢迎的免费编程课程
    微服务架构师封神之路13-RabbitMQ集群与高可用|RabbitMQ clustering and HA
    【状语从句练习题】although vs but
    体验Semantic Kernel图片内容识别
    中小企业数字化转型的三条建议
  • 原文地址:https://blog.csdn.net/qq_59183443/article/details/127903546