Post-order Calculation
/*
Time limit: 1000ms
Memory limit: 256mb
---------------------Copy the following code, complete it and submit---------------------
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct treeNode {
int value;
struct treeNode* left;
struct treeNode* right;
};
typedef struct treeNode* TreeNode;
// Create a new tree node with the given value
TreeNode CreateTreeNode(int value) {
TreeNode node = (TreeNode) malloc(sizeof(struct treeNode));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
TreeNode Lchind(TreeNode root) {
return (root != NULL) ? root->left : NULL;
}
TreeNode Rchind(TreeNode root) {
return (root != NULL) ? root->right : NULL;
}
bool isEmpty(TreeNode root) {
return (root == NULL);
}
// Build a binary tree from the given PreOrder and InOrder traversal
TreeNode BuildBinaryTree(int* PreOrder, int* InOrder, int n) {
// WRITE YOUR CODE HERE
}
// Print the binary tree in PostOrder
void PostOrderPrint(TreeNode root) {
if (root == NULL) {
return;
}
PostOrderPrint(root->left);
PostOrderPrint(root->right);
printf("%d ", root->value);
}
int main()
{
int n;
scanf("%d", &n);
int* PreOrder = (int*) malloc(n * sizeof(int));
int* InOrder = (int*) malloc(n * sizeof(int));
for (int i = 0; i < n; ++i) {
scanf("%d", PreOrder + i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", InOrder + i);
}
TreeNode root = BuildBinaryTree(PreOrder, InOrder, n);
if (root == NULL) {
printf("Invalid Input.\n");
return 0;
}
PostOrderPrint(root);
return 0;
}
---------------------------------------End of Code---------------------------------------
## [Post-order Calculation]
You are given the **pre-order and in-order** traversal sequences of a binary tree. Your task is to reconstruct the binary tree and output its **post-order** traversal sequence.
Notice that If the sequences are invalid (i.e., they do not correspond to any valid binary tree), output an error message.
### Input
The first line contains an integer $n(1\leq n\leq 3000)$ — the number of nodes in the tree.
The second line contains $n$ **unique** integers $a_i (1\leq a_i \leq n)$, representing the **pre-order traversal** sequence.
The third line contains $n$ **unique** integers $b_i (1\leq b_i \leq n)$, representing the **in-order traversal** sequence.
### Output
Print the **post-order traversal** sequence of the reconstructed binary tree as $n$ space-separated integers.
If the sequences are invalid, output , output "Invalid Input."
### Example 1
**Input**
```
5
3 1 2 5 4
1 3 5 2 4
```
**Output**
```
1 5 4 2 3
```
### Example 2
**Input**
```
4
1 2 4 3
2 3 1 4
```
**Output**
```
Invalid Input.
```
**Explanation**
In Example 1, the reconstructed tree is:
3
/ \
1 2
/ \
5 4
**Post-order traversal** visits nodes as: 1 → 5 → 4 → 2 → 3.
In Example 2, the input is invalid, as a result the output is "Invalid Input."
Submit