+ 2

Is there any way to represent a binaary tree as singly linked list

I was asked this question in an interview how will you represent a binary tree as linked list(singly) , i still havent figured out.. plese help me

18th Jul 2019, 3:47 AM
SWAPNIL DATTARAY SASTE
SWAPNIL DATTARAY SASTE - avatar
3 odpowiedzi
+ 4
If a singly linked list only has one outgoing link I don't think it is possible to do this while maintaining the tree structure. Of course you could probably serialize the tree via a depth or breadth first traversal but then you would lose the tree structure.
18th Jul 2019, 4:04 AM
Sonic
Sonic - avatar
+ 3
You can always traverse a binary tree in ascending order of elements, because the left node is always smaller than the right. So I think that would be a way to start from the smallest element (first go left until you reach a leaf) and 'collect' all leafs into the singly linked list on ascending order. Reversing this process does not seem possible though (from list back to the same binary tree).
18th Jul 2019, 5:04 AM
Tibor Santa
Tibor Santa - avatar