Serialize and Deserialize Binary Tree
Hardjavapythonccppjavascript
Build a binary tree from its level-order description, then serialize it back to a canonical level-order form: perform a breadth-first walk emitting each node's value, and null for every missing child of a real node. Trailing null tokens are removed.
Input Format
The first line contains an integer M, the number of level-order tokens. The second line contains M space-separated tokens (integers or null).
Output Format
Print the canonical serialization on one line, tokens separated by single spaces. Print an empty line for an empty tree.
Example 1
Input
7 1 2 3 null null 4 5
Output
1 2 3 null null 4 5
Explanation: The rebuilt tree serializes to the same canonical form.
- 0 <= number of nodes <= 5000
- -1000000000 <= node value <= 1000000000
Submit an attempt to unlock the solution. (Sign in first.)
Related Problems
- Maximum Path Sum in Binary TreeHard · java, python, c, cpp, javascriptSolve Problem
- Boundary TraversalHard · java, python, c, cpp, javascriptSolve Problem
- Vertical Order TraversalHard · java, python, c, cpp, javascriptSolve Problem