Class Solution
- java.lang.Object
-
- g1001_1100.s1028_recover_a_tree_from_preorder_traversal.Solution
-
public class Solution extends Object
1028 - Recover a Tree From Preorder Traversal.Hard
We run a preorder depth-first search (DFS) on the
rootof a binary tree.At each node in this traversal, we output
Ddashes (whereDis the depth of this node), then we output the value of this node. If the depth of a node isD, the depth of its immediate child isD + 1. The depth of therootnode is0.If a node has only one child, that child is guaranteed to be the left child.
Given the output
traversalof this traversal, recover the tree and return itsroot.Example 1:

Input: traversal = “1-2–3–4-5–6–7”
Output: [1,2,5,3,4,6,7]
Example 2:

Input: traversal = “1-2–3—4-5–6—7”
Output: [1,2,5,3,null,6,null,4,null,7]
Example 3:

Input: traversal = “1-401–349—90–88”
Output: [1,401,null,349,88,90]
Constraints:
- The number of nodes in the original tree is in the range
[1, 1000]. 1 <= Node.val <= 109
- The number of nodes in the original tree is in the range
-
-
Constructor Summary
Constructors Constructor Description Solution()
-