• 1162 Postfix Expression


    Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:

    data left_child right_child
    

    where data is a string of no more than 10 characters, left_child and right_child are the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.

    Figure 1Figure 2

    Output Specification:

    For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.There must be no space between any symbols.

    Sample Input 1:

    1. 8
    2. * 8 7
    3. a -1 -1
    4. * 4 1
    5. + 2 5
    6. b -1 -1
    7. d -1 -1
    8. - -1 6
    9. c -1 -1

    Sample Output 1:

    (((a)(b)+)((c)(-(d))*)*)
    

    Sample Input 2:

    1. 8
    2. 2.35 -1 -1
    3. * 6 1
    4. - -1 4
    5. % 7 8
    6. + 2 3
    7. a -1 -1
    8. str -1 -1
    9. 871 -1 -1

    Sample Output 2:

    (((a)(2.35)*)(-((str)(871)%))+)

    题目大意 

    给定一个二叉表达式树,请你输出相应的后缀表达式,要求使用括号反映运算符的优先级

    注意点

    三种情况:

    ①左右孩子都不存在时 ,直接访问当前节点

    ②左右孩子都存在时 进行后序遍历 即:左->右->根

    ③左孩子不存在右孩子存在时,进行先序遍历即 :根->右 

    AC代码

    1. /*
    2. * @Author: Spare Lin
    3. * @Project: AcWing2022
    4. * @Date: 2022/6/28 14:28
    5. * @Description: PAT (Advanced Level) 1162 Postfix Expression
    6. */
    7. #include <iostream>
    8. #include <algorithm>
    9. using namespace std;
    10. const int MAXN = 25;
    11. typedef struct {
    12. int l, r; //左右孩子
    13. string v; //权值
    14. } Node;
    15. Node node[MAXN];
    16. int n, root, flag[MAXN];
    17. void dfs(int index) {
    18. cout << '(';
    19. //左右孩子都不存在 直接输出节点权值
    20. if (node[index].l == -1 && node[index].r == -1) {
    21. cout << node[index].v;
    22. }
    23. //左右孩子都存在 则进行后序遍历
    24. else if (node[index].l != -1 && node[index].r != -1) {
    25. dfs(node[index].l);
    26. dfs(node[index].r);
    27. cout << node[index].v;
    28. }
    29. //样例中的-作为负号时 进行先序遍历 即左孩子不存在 右孩子存在 先访问节点权值再访问右孩子
    30. else {
    31. cout << node[index].v;
    32. dfs(node[index].r);
    33. }
    34. cout << ')';
    35. }
    36. int main() {
    37. cin >> n;
    38. for (int i = 1; i <= n; ++i) {
    39. cin >> node[i].v >> node[i].l >> node[i].r;
    40. if (node[i].l != -1) flag[node[i].l] = 1; //当前结点左孩子是否存在
    41. if (node[i].r != -1) flag[node[i].r] = 1; //当前结点右孩子是否存在
    42. }
    43. //遍历找根节点
    44. for (int i = 1; i <= n; ++i) {
    45. if (flag[i] == 0) {
    46. root = i;
    47. }
    48. }
    49. dfs(root);
    50. return 0;
    51. }

     

  • 相关阅读:
    Windows服务器安装php+mysql环境的经验分享
    IO学习系列之使用fgetc函数实现Linux命令“wc -l”和“wc -c”的功能
    Tomcat安装与配置(详细教程)
    Linux 之 journalctl 查看系统与 kernel 日志
    【毕业季·进击的技术er】大学生计算机毕业设计应该这样写
    ZigBee案例笔记 -- IAR for 8051工程创建
    Mac下载安装配置运行MySQL
    洛谷P2261 整除分块模板
    SpringMvc拦截器
    静态成员变量和成员函数
  • 原文地址:https://blog.csdn.net/weixin_55664293/article/details/125508053