Loading...
Engaged Employer
collapse a binary search tree into a sorted list
Anonymous
In-order traversal of the tree should do it.
PseudoCode Collapse(node N, list L) { if (N.left) Collapse(N.left) L.add(node.value) if (N.right) Collapse(N.right) } List SL; Collapse(root, SL);
#!/usr/bin/python3 def collapse(binarytree, sorted_list = []): if binarytree: collapse(binarytree.left) sorted_list.append(binarytree.value) collapse(binarytree.right) return sorted_list print(collapse(binarytree))
depth first search
Check out your Company Bowl for anonymous work chats.
Get actionable career advice tailored to you by joining more bowls.
Stay ahead in opportunities and insider tips by following your dream companies.
Get personalized job recommendations and updates by starting your searches.