How to Serialize Binary Tree

后端 未结 10 897
野的像风
野的像风 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:50

    Approach 1: Do both Inorder and Preorder traversal to searialize the tree data. On de-serialization use Pre-order and do BST on Inorder to properly form the tree.

    You need both because A -> B -> C can be represented as pre-order even though the structure can be different.

    Approach 2: Use # as a sentinel whereever the left or right child is null.....

提交回复
热议问题