-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
62 lines (56 loc) · 1.48 KB
/
index.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
56
57
58
59
60
61
62
const { expect } = require('chai');
const { buildTree } = require('../util/javascript/problem-utils');
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/**
* @param {string} S
* @return {TreeNode}
*/
let recoverFromPreorder = function (S) {
let nodes = [];
let len = 0, word = '';
for (let i = 0; i < S.length; i++) {
if ('-' === S[i]) {
if ('-' === S[i - 1]) len++;
else {
nodes.push([len, word]);
len = 1;
word = '';
}
} else {
word += S[i];
}
}
nodes.push([len, word]);
const traverse = (ns, depth) => {
const node = new TreeNode(ns[0][1]);
let left, right;
for (let i = 1; i < ns.length; i++) {
if (ns[i][0] === depth + 1) {
if (left === undefined) left = i;
else right = i;
}
}
if (left !== undefined) {
if (right === undefined) node.left = traverse(ns.slice(left), depth + 1);
else node.left = traverse(ns.slice(left, right), depth + 1);
}
if (right !== undefined) node.right = traverse(ns.slice(right), depth + 1);
return node;
};
return traverse(nodes, 0);
};
describe('', () => {
it('recover-a-tree-from-preorder-traversal', () => {
expect(JSON.stringify(recoverFromPreorder('1-2--3--4-5--6--7'))).to.eq(JSON.stringify(buildTree(['1', '2', '5', '3', '4', '6', '7'])));
});
});