问题
Im trying to solve this problem:" A binary tree is given, check his pre-order visit and build a binary search tree with the same pre-order visit. Demonstrate if it is always possible, if not give an example when this is not possible." Any help? I need to write pseudocode and give time complexity but i have a lot of doubts about building a binary-search-tree with the same pre-order visit for every possible binary tree.
回答1:
If you are using the classic algorithm for inserting in a binary search tree, that is, to perform a search and on the found NULL
pointer where the search was stopped to put the new node, then just to insert in an empty tree the preorder sequence will produce exactly a binary tree with exactly the given preorder sequence.
Just try. Traverse any preorder sequence and insert it in an empty tree and you will realize it.
I hope to help you. And welcome to stack overflow!
来源:https://stackoverflow.com/questions/54170577/given-a-pre-order-binary-tree-visit-construct-a-binary-search-tree-with-the-same