首页 > 代码库 > Serialize a Binary Tree or a General Tree

Serialize a Binary Tree or a General Tree

For a binary tree, preorder traversal may be enough.

 

For example, 

    _30_
   /    \   
  10    20
 /     /  \
50    45  35
The result is 

30 10 50 # # # 20 45 # # 35 # #
Using a queue to deserialize it . 

But a for multi-way tree, we could also use an array to serialize it, e.g., 

30 10 20 50 45 35

-1   0    0   1    2   2



Serialize a Binary Tree or a General Tree