如标题所述,我有一棵二叉搜索树。我想使用递归将其转换为排序的双向链表。
我的代码:
但这种解决方案效率不高,因为它会多次到达每个节点。在我的优化代码探索中,我从谷歌获得了一个链接 greatTreeList 解决方案。我在 SO 中搜索了相同的内容,两种解决方法都适用于我。我没有理解解决方案中的
for each node in tree
find max of left sub-tree and assign its right to present node ,present node left to max
find min of right sub-tree and assign its left to present node ,present node right to max
and now recursively do same thing to other nodes in BST .
但这种解决方案效率不高,因为它会多次到达每个节点。在我的优化代码探索中,我从谷歌获得了一个链接 greatTreeList 解决方案。我在 SO 中搜索了相同的内容,两种解决方法都适用于我。我没有理解解决方案中的
append function
,因为它包含代码。join(alast,b)
join(blast,a)
对于按照以下顺序插入节点的树10,5,9,6,8,7,12,11,13,14
请问有人能解释一下如何在每次递归调用中链接节点的join(alast,b)和join(blast,a)吗?