How to Serialize Binary Tree

后端 未结 10 847
野的像风
野的像风 2020-12-23 21:09

I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order trave

10条回答
  •  生来不讨喜
    2020-12-23 21:38

    The best way is to use a special char (like # as previous comment mentioned) as sentinel. It's better than constructing an inorder traversal array and a preorder/postorder traversal array, both in space complexity wise and time complexity wise. it's also way easier to implement.

    Linked list is not a good fit here since in order to reconstruct the tree, you better have const element access time

提交回复
热议问题