At first, store the pointer to the first node in inorder traversal (in this
case 5) because its going to be the head of the list.
Then use the following logic.
flattenTree(TreeNode node) {
if (node is leaf node)
return node;
if (node.left exists) then
flattenTree(node.left)
No, you are wrong here.
The inorder sequence should be 5 -> 25 -> 30 -> 50 -> 55 -> 60 ->75.
The preorder sequence should be 50 -> 25 -> 5 -> 30 -> 60 -> 55 -> 75
The postorder sequence should be 5 -> 30 -> 25 -> 55 -> 75 -> 60 -> 50
Below is the analysis (from wikipedia):
To traverse a non-em
Am 26.08.2010 18:59, schrieb krazee koder:
> Give all possible methods to flatten a binary tree to a linked list.
>
> for eg.
>
>50
> / \
> 25 60
> / \ / \
> 530 55 75
>
>
> should be flattened to 5->25->30->50->55->60->75
>
> PS: note that the tree should be
I can only figure out the inorder traversal...
On Thu, Aug 26, 2010 at 9:59 AM, krazee koder wrote:
> Give all possible methods to flatten a binary tree to a linked list.
>
> for eg.
>
> 50
> / \
> 25 60
> / \ / \
> 5 30 55 75
>
>
> should be flattened to 5->25-
Give all possible methods to flatten a binary tree to a linked list.
for eg.
50
/ \
25 60
/ \ / \
530 55 75
should be flattened to 5->25->30->50->55->60->75
PS: note that the tree should be converted to the LL and no separate
LL should be formed.
--
You