|
| 1 | +// Source : https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/ |
| 2 | +// Author : Ahmed Morsy |
| 3 | +// Date : 2019-05-29 |
1 | 4 | // source : https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/
|
2 |
| -// Author Ahmed Morsy |
3 |
| -// Date : 2019-5-19 |
4 |
| - |
5 |
| -/********************************************************************************** |
6 |
| -*We run a preorder depth first search on the root of a binary tree. |
7 |
| -*At each node in this traversal, we output D dashes (where D is the depth of this node), |
8 |
| -*then we output the value of this node. |
9 |
| -*(If the depth of a node is D, the depth of its immediate child is D+1.The depth of the root node is 0.) |
10 |
| -*If a node has only one child, that child is guaranteed to be the left child. |
11 |
| -*Given the output S of this traversal, recover the tree and return its root. |
12 |
| -
|
13 |
| -* Input: "1-2--3--4-5--6--7" |
14 |
| -* Output: [1,2,5,3,4,6,7] |
15 |
| -
|
16 |
| -* Proposed Solution |
17 |
| - ----------------- |
18 |
| - 1. for each node in the input save its depth and value |
19 |
| - 2. start a stack with the root node pushed onto it |
20 |
| - |
21 |
| - IF the following node has the root's depth + 1 and the root has less than 2 children then this node |
22 |
| - is a child for this root. Add it to the stack. |
23 |
| - |
24 |
| - ELSE then pop the current node from the stack. |
25 |
| - |
26 |
| -**********************************************************************************/ |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * We run a preorder depth first search on the root of a binary tree. |
| 8 | + * |
| 9 | + * At each node in this traversal, we output D dashes (where D is the depth of this node), then we |
| 10 | + * output the value of this node. (If the depth of a node is D, the depth of its immediate child is |
| 11 | + * D+1. The depth of the root node is 0.) |
| 12 | + * |
| 13 | + * If a node has only one child, that child is guaranteed to be the left child. |
| 14 | + * |
| 15 | + * Given the output S of this traversal, recover the tree and return its root. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * |
| 19 | + * Input: "1-2--3--4-5--6--7" |
| 20 | + * Output: [1,2,5,3,4,6,7] |
| 21 | + * |
| 22 | + * Example 2: |
| 23 | + * |
| 24 | + * Input: "1-2--3---4-5--6---7" |
| 25 | + * Output: [1,2,5,3,null,6,null,4,null,7] |
| 26 | + * |
| 27 | + * Example 3: |
| 28 | + * |
| 29 | + * Input: "1-401--349---90--88" |
| 30 | + * Output: [1,401,null,349,88,90] |
| 31 | + * |
| 32 | + * Note: |
| 33 | + * |
| 34 | + * The number of nodes in the original tree is between 1 and 1000. |
| 35 | + * Each node will have a value between 1 and 10^9. |
| 36 | + * |
| 37 | + ******************************************************************************************************/ |
27 | 38 |
|
28 | 39 | /* Definition for a binary tree node.
|
29 | 40 | struct TreeNode {
|
|
0 commit comments