Objective
There are three classic ways to traverse a binary tree recursively: preorder (node, left, right), inorder (left, node, right) and postorder (left, right, node). The first line has the number of nodes n; then come n lines value left right, where left and right are the indexes (from 0) of the children or -1 if there is no child. Node 0 is the root.
Instructions
- Show three lines:
Preorder: ...,Inorder: ...andPostorder: ..., each with the values in that traversal separated by spaces.
5
1 1 2
2 3 4
3 -1 -1
4 -1 -1
5 -1 -1
Preorder: 1 2 4 5 3
Inorder: 4 2 5 1 3
Postorder: 4 5 2 3 1
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise