-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1028-recover-a-tree-from-preorder-traversal.js
55 lines (50 loc) · 1.55 KB
/
1028-recover-a-tree-from-preorder-traversal.js
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
/**
* 1028. Recover a Tree From Preorder Traversal
* https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/
* Difficulty: Hard
*
* 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.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {string} traversal
* @return {TreeNode}
*/
var recoverFromPreorder = function(traversal) {
const stack = [];
for (let i = 0; i < traversal.length;) {
let depth = 0;
while (traversal[i] === '-') {
depth++;
i++;
}
let value = 0;
while (i < traversal.length && traversal[i] !== '-') {
value = value * 10 + parseInt(traversal[i]);
i++;
}
const node = new TreeNode(value);
while (stack.length > depth) stack.pop();
if (stack.length) {
if (!stack[stack.length - 1].left) stack[stack.length - 1].left = node;
else stack[stack.length - 1].right = node;
}
stack.push(node);
}
return stack[0];
};