-->
当前位置:首页 > 题库

PROGRAMMING:Binary tree traversal( Simple)

Luz5年前 (2021-05-10)题库443
As one of the core data structures of FDS course, binary tree requires everyone to master!
This is a simple binary tree problem!
We will give a binary tree, please output its three kinds of traversal, which are preorder traversal, middle order traversal, and postorder traversal!
###Input format:
The binary tree will be given in this form
The first line gives a positive integer n (n < = 100), indicating the number of nodes on the binary tree!
Next N lines, each line contains three integers, I, l, R, representing the left and right children of the i-th node respectively!
If its left / right child is empty, give - 1 in the corresponding position!
Topic 1 is the root node!
###Output format:
Please output its three traversals!
The first line output first order traversal, the second line output middle order traversal, the third line output after order traversal!
No extra space at the end of each line!
###Input example:
Here is a set of inputs. For example:
```in
three
1 2 3
2 -1 -1
3 -1 -1
```
###Output example:
The corresponding output is given here. For example:
```out
1 2 3
2 1 3
2 3 1
```







answer:If there is no answer, please comment