-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22_Recover_a_Tree_From_Preorder_Traversal.cpp
80 lines (65 loc) · 2.34 KB
/
22_Recover_a_Tree_From_Preorder_Traversal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 1028. Recover a Tree From Preorder Traversal
// We run a preorder depth-first search (DFS) on the root of a binary tree.
// At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node. 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.
// If a node has only one child, that child is guaranteed to be the left child.
// Given the output traversal of this traversal, recover the tree and return its root.
// 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
class Solution
{
public:
string s;
int idx = 0, level = 0;
TreeNode *recoverFromPreorder(string traversal)
{
s = traversal;
TreeNode *node = new TreeNode(-1);
helper(node, 0);
return node->left;
}
void helper(TreeNode *parent, int lvl)
{
while (idx < s.length() && lvl == level)
{
int num = 0;
while (idx < s.length() && isdigit(s[idx]))
{
num = num * 10 + (s[idx++] - '0');
}
TreeNode *node = new TreeNode(num);
if (!parent->left)
parent->left = node;
else
parent->right = node;
level = 0;
while (idx < s.length() && s[idx] == '-')
{
level++;
idx++;
}
helper(node, lvl + 1);
}
}
};
// Main Function - recoverFromPreorder:
// - Creates a dummy root node with value -1
// - Calls helper function to build the tree
// - Returns the left child of dummy node (actual root)
// Helper Function - helper:
// - Takes parent node and current level as parameters
// - Works recursively to build the tree:
// - Extracts number from string by parsing digits
// - Creates new node with parsed value
// - Attaches node as left child if no left child exists, otherwise as right child
// - Counts consecutive dashes to determine next level
// - Recursively builds subtrees