Given a pre-order binary tree visit construct a binary search tree with the same pre-order visit. (if possible)

不羁的心 提交于 2019-12-11 04:49:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!