Is this the right strategy to convert an in-level order binary tree to a doubly linked list?

Posted by Ankit Soni on Programmers See other posts from Programmers or by Ankit Soni
Published on 2011-09-20T07:26:37Z Indexed on 2011/11/12 18:06 UTC
Read the original article Hit count: 323

Filed under:
|
|
|

So I recently came across this question - Make a function that converts a in-level-order binary tree into a doubly linked list. Apparently, it's a common interview question.

This is the strategy I had - Simply do a pre-order traversal of the tree, and instead of returning a node, return a list of nodes, in the order in which you traverse them. i.e return a list, and append the current node to the list at each point. For the base case, return the node itself when you are at a leaf.

so you would say

left = recursive_function(node.left)
right = recursive_function(node.right)
return(left.append(node.data)).append(right);`

Is this the right approach?

© Programmers or respective owner

Related posts about interview

Related posts about recursion