-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.js
84 lines (79 loc) · 2.05 KB
/
3.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回
* 前序遍历(VLR):
1.访问根节点 (node)
2.前序遍历左子树 (leftChild)
3.前序遍历右子树 (rightChild)
中序遍历(LVR):
1.中序遍历左子树 (leftChild)
2.访问根节点 (node)
3.中序遍历右子树 (rightChild)
后序遍历(LRV):
1.后序遍历左子树 (leftChild)
2.后序遍历右子树 (rightChild)
3.访问根节点 (node)
*/
/*
function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
}
*/
let res = [];
const tree = {
val: 1,
left: {
val: 2,
left: null,
right: null
},
right: {
val: 3,
left: {
val: 4,
left: null,
right: null
},
right: {
val: 5,
left: null,
right: null
},
}
}
// VLR
function VLR(tree) {
res.push(tree.val);
if (tree.left) VLR(tree.left);
if (tree.right) VLR(tree.right);
}
// LVR
function LVR(tree) {
if (tree.left) LVR(tree.left);
res.push(tree.val);
if (tree.right) LVR(tree.right);
}
// LRV
function LRV(tree) {
if (tree.left) LRV(tree.left);
if (tree.right) LRV(tree.right);
res.push(tree.val);
}
function reConstructBinaryTree(pre, vin) {
if (pre.length === 0 || vin.length === 0) {
return null;
}
const index = vin.indexOf(pre[0]);
const left = vin.slice(0, index);
const right = vin.slice(index + 1);
// Get area of left tree
const leftArr = pre.slice(1, index + 1);
// Get area of right tree
const rightArr = pre.slice(index + 1);
return {
val: pre[0],
left: reConstructBinaryTree(leftArr, left),
right: reConstructBinaryTree(rightArr, right),
}
}