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

PROGRAMMING:Deleting binary search tree

Luz5年前 (2021-05-10)题库428
Given a binary search tree (without the same elements), please output its traversal sequence after deleting some elements.
The strategy of deleting nodes is as follows:
*If a node is a leaf node, it will be deleted directly;
*If the left subtree of a node is not empty, set the value of the node to the maximum value of each node in its left subtree, and continue to delete the node with the maximum value in its left subtree;
*If the left subtree of a node is empty but the right subtree is not, set the value of the node to the minimum value of each node in its right subtree, and continue to delete the node with the minimum value in its right subtree.
###Input format:
Each input file contains a test case. The first line of each test case contains an integer $$n $$($$0 < n < = 100 $$), which represents the number of nodes in the binary search tree.
The second line gives the sequence of the binary search tree, which is composed of $$n $$integers separated by a space. The third line gives an integer, $$k $$($$0 < K < n $$), indicating the number of nodes to be deleted. The last line gives $$k $$integers, which represent the values on each node to be deleted. Nodes must be deleted in order of input. The topic ensures that the node can be deleted.
###Output format:
Output the sequence traversal sequence after deleting nodes in one line. The numbers in the sequence are separated by a space, and there must be no extra space at the end of the line.
###Input example:
```in
seven
4 2 1 3 6 5 7
two
3 6
```
###Output example:
```out
4 2 5 1 7
```







answer:If there is no answer, please comment