Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
问题描述:(语言为java)
给定一个表示二叉搜索树的前序遍历的输入数组,输出 BST 的后序遍历。
抓住 :
我已经尝试了几个小时,但仍然没有任何线索。 最难的部分是不使用树节点结构。 有人有想法吗?
这是一个提示:
在preorder遍历中,数组的第一个元素总是树的根,所以如果NodeElemnts[]给定数组,那么NodeElemnts[0] 将是树的根
preorder
NodeElemnts[]
NodeElemnts[0]
那么,左节点位于 ,2n+1右节点位于2n+2,n是当前数组的索引。
2n+1
2n+2
n
对于前到后:
[root][leftChild][rightChild](前)-> [leftChild][rightChild][root](后).....想想BFS :)
[root][leftChild][rightChild]
[leftChild][rightChild][root]