# | Problem | Pass Rate (passed user / total user) |
---|---|---|
11833 | Construct a binary tree |
|
11847 | Prefix Boolean expression |
|
Description
Niflheimr discovers an interesting fact: trees grow upward in real world, but trees grow downward in CS world.
He decides to study the trees in CS world, so he choose the simplest one for his study: binary tree. Before study begins, he needs to construct a binary tree first.
Given a in-order traversal sequence and a pre-order traversal sequence of a binary tree, construct the binary tree and print the post-order traversal sequence of that binary tree.
Hint:
1. If you have no idea how to implement, please take it as your reference.
function.h
#include <stdlib.h> #include <stdio.h> typedef struct _node { int value; struct _node *left; struct _node *right; } Node; Node *create_node(int value); void postorder(Node *root); void destroy(Node* root); /* Parameter description: int *inorder : the inorder traversal sequence of the binary tree. int *preorder : the preorder traversal sequence of the binary tree. int inorder_start : the starting index of the inorder traversal of the subtree. int inroder_end : the ending index of the inorder traversal of the subtree. As for the preorder traversal index, try declaring a static variable inside this function. */ Node *build(int *inorder, int *preorder, int inorder_start, int inorder_end);
2. If you get a TLE in the last testcase, try to think over the property underlined in "Input" section.
Input
The first line of input contains a integer N, indicating the number of nodes on the tree.
The second line contains N distinct numbers, which is the in-order traversal sequence of the binary tree.
The third line contains N distinct numbers, which is the pre-order traversal sequence of the binary tree.
It is guaranteed that:
- 1 ≤ N ≤ 2*105
- For all x being the number on the binary tree, 0 ≤ x ≤ N-1.
Output
Print out the post-order traversal sequence of the binary tree.
There is a white space after each number (including the last one). For example, [0 1 2 3 ].
Remember to print a '\n' at the end of the output.
Sample Input Download
Sample Output Download
Discuss
Description
Give a prefix Boolean expression, which only has at most 4 variables ‘A’, ‘B’, ‘C’, and ‘D’, and 2 operators, AND ‘&’ and OR ‘|’, print its truth table.
For example, if input is "|&AC|AB", then result will be
A B C D output
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 0 1 1 0
0 1 0 0 1
0 1 0 1 1
0 1 1 0 1
0 1 1 1 1
1 0 0 0 1
1 0 0 1 1
1 0 1 0 1
1 0 1 1 1
1 1 0 0 1
1 1 0 1 1
1 1 1 0 1
1 1 1 1 1
Input
The input contains a sequences of prefix expression. It only has at most 4 variables ‘A’, ‘B’, ‘C’, and ‘D’, and 2 operators, AND ‘&’ and OR ‘|’. The length of prefix expression is shorter than 30.
Output
It has 5 variables 'A', 'B', 'C', 'D', and output, each variable is separated by a space.