如同 A1130 Infix Expression 树的遍历判断
1. 原文
Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.
1.0.1. 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:
1 | 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.
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 |
Sample Output 1
1 | (((a)(b)+)((c)(-(d))*)*) |
Sample Input 2
1 | 8 |
Sample Output 2
1 | (((a)(2.35)*)(-((str)(871)%))+) |
2. 解析思路
类比 A1130
采用后序遍历方法,当左子树存在时,采用左右根的遍历方法,当左子树不存在时,采用根右的遍历方法
3. AC代码
1 |
|