TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Great Tree-List Recursion Problem (2000)

70 pointsby nickdrozdover 3 years ago

7 comments

mbf1over 3 years ago
I added next and previous pointers to my red-black tree implementation for a freshman-level CS class back in 1997. It enabled me to claim O(1) rather than O(lg N) to &quot;find next value&quot; at an expense of a constant order of memory usage. Converting the whole tree to a doubly linked list in O(N) time is cute. It&#x27;s also possible to re-construct a new balanced binary search tree in O(N) time using O(N) memory.<p>You&#x27;d start by counting the number of nodes in your circular array O(N), and making an array of pointers to each node O(N), then finding the middle node, which is the root node at count &#x2F; 2. You&#x27;d then re-link the nodes for a sub-array and recurse on both sides using your in-order array of pointers, and replacing the smaller and larger node pointers with the pointers from the array. The total recursive algorithm visits each node in the new tree only once.
dangover 3 years ago
Some past threads:<p><i>The Great Tree-List Recursion Problem</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=19324604" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=19324604</a> - March 2019 (21 comments)<p><i>The Great Tree-List Recursion Problem (2000)</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=15347519" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=15347519</a> - Sept 2017 (38 comments)
jhallenworldover 3 years ago
This reminds of a similar problem: visit all nodes of a tree without using recursion or an explicit stack (or any extra storage). It&#x27;s useful for marking live nodes during mark &amp; sweep garbage collection with a guarantee that the mark process itself will complete and not cause you to run out of memory.<p>So this is my amended problem: convert the tree to a same-ordered doubly-linked list without using recursion or an explicit stack.
评论 #29074166 未加载
评论 #29074032 未加载
评论 #29073325 未加载
评论 #29073456 未加载
评论 #29073277 未加载
icambronover 3 years ago
I took a quick crack at this [1] and I have a few comments:<p>1. The double-linking and circularity make the solution uglier and more fiddly without making it really conceptually any harder. In particular, it means you end up mixing recursion with mutation, which is a little gunky. I wish they&#x27;d just have you output a single-linked list: same bang, less buck.<p>2. The problem mentions that the double-linked list nodes look a lot like the tree nodes, in the sense that they&#x27;re a value with two pointers, but in practice that didn&#x27;t have any effect on the solution, and I&#x27;m not really sure it&#x27;s very interesting.<p>3. A thing that always comes up in these kinds of recursion problems is where to put the null checks, which in this case are a little complicated. I think neither my solution and the Java one in the link do a great job on that front<p>[1] <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;icambron&#x2F;4c03e1b19c3ca795d3930ed1720684af" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;icambron&#x2F;4c03e1b19c3ca795d3930ed1720...</a>
评论 #29076025 未加载
hvasilevover 3 years ago
I thought that flattening an ordered binary tree to a double-linked list is a very common question asked on job interviews. Personally I&#x27;ve seen the question twice on interviews so far.
评论 #29075053 未加载
anonyglerover 3 years ago
This is just a LeetCode Medium. Amazed how twisted that site has made my brain. 2000 was so innocent.
评论 #29078478 未加载
cabalamatover 3 years ago
If I had to do this in production code I wouldn&#x27;t do it that way (for a start I prefer static data structures where possible). I&#x27;d have 2 classes: BinaryTree and DoubleLinkedList, each of which would have a constructor that takes a list as input, and a function that returns the data as a list:<p><pre><code> class BinaryTree: def __init__(self, data: List): &quot;&quot;&quot; create from a list &quot;&quot;&quot; def asList(self)-&gt;List: &quot;&quot;&quot; return my data as a list &quot;&quot;&quot; class DoubleLinkedList: def __init__(self, data: List): &quot;&quot;&quot; create from a list &quot;&quot;&quot; def asList(self)-&gt;List: &quot;&quot;&quot; return my data as a list &quot;&quot;&quot; </code></pre> Then making a binary tree from a double-linked list is simply:<p><pre><code> BinaryTree(dll.asList())</code></pre>
评论 #29075842 未加载