-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-120-buildTreeInPost-2.js
141 lines (118 loc) · 3.88 KB
/
structy-120-buildTreeInPost-2.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
// p: arr, arr
// r: root
// [ 'z' ],
// [ 'z', ]
// x
// / \
// y z
const buildTreeInPost = (
i,
p,
iS = 0,
iE = i.length - 1,
pS = 0,
pE = p.length - 1
) => {
if (iS > iE || pS > pE) return null;
const rootVal = p[pE];
const indexRootI = i.indexOf(rootVal);
const rightLen = iE - indexRootI;
const root = new Node(rootVal);
root.left = buildTreeInPost(i, p, iS, indexRootI - 1, pS, pE - rightLen - 1);
root.right = buildTreeInPost(i, p, indexRootI + 1, iE, pE - rightLen, pE - 1);
return root;
};
console.log(
// buildTreeInPost(
// ["d", "b", "g", "e", "h", "a", "c", "f"],
// ["d", "g", "h", "e", "b", "f", "c", "a"]
// )
buildTreeInPost(["y", "x", "z"], ["y", "z", "x"])
);
// //
// // [ 'd', 'b', 'e', /'a',/ 'f',// 'c',// 'g' ],
// // [ 'd', 'e', 'b', 'f', 'g', //'c', /'a' ]
// // a
// // / \
// // b c
// // / \ / \
// // d e f g
// // [ 'd', 'b', 'g', 'e', 'h',/ 'a', / 'c', // 'f' ],
// // [ 'd', 'g', 'h', 'e', 'b', 'f', //'c', /'a' ]
// // a
// // / \
// // b c
// // / \ \
// // d e f
// // / \
// // g h
// const buildTreeInPost = (inOrder, postOrder) => {
// if (!inOrder.length || !postOrder.length) return null;
// const indexRootPo = postOrder.length - 1;
// const indexRPo = indexRootPo - 1;
// const rootVal = postOrder[indexRootPo];
// const indexRootIn = inOrder.indexOf(rootVal);
// const rightLen = inOrder.length - 1 - indexRootIn;
// const indexLPo = indexRPo - rightLen;
// const root = new Node(rootVal);
// // console.log(root);
// root.left = buildTreeInPost(
// inOrder.slice(0, indexRootIn),
// postOrder.slice(0, indexLPo + 1)
// );
// root.right = buildTreeInPost(
// inOrder.slice(indexRootIn + 1),
// postOrder.slice(indexLPo + 1, indexRootPo)
// );
// return root;
// };
// // recu O(n^2) O(n^2)
// const buildTreeInPost = (inOrder, postOrder) => {
// if (inOrder.length === 0 || postOrder.length === 0) return null;
// const rootVal = postOrder[postOrder.length - 1];
// const root = new Node(rootVal);
// const indexIn = inOrder.indexOf(rootVal);
// const leftInOrder = inOrder.slice(0, indexIn);
// const leftPostOrder = postOrder.slice(0, leftInOrder.length);
// const rightInOrder = inOrder.slice(indexIn + 1);
// const rightPostOrder = postOrder.slice(leftInOrder.length, -1);
// root.left = buildTreeInPost(leftInOrder, leftPostOrder);
// root.right = buildTreeInPost(rightInOrder, rightPostOrder);
// return root;
// };
// const buildTreeInPost = (inOrder, postOrder) => {
// if (inOrder.length === 0 || postOrder.length === 0) return null;
// const indexPost = postOrder.length - 1;
// const rootVal = postOrder[indexPost];
// // console.log(indexPost);
// const root = new Node(rootVal);
// const indexIn = inOrder.indexOf(rootVal);
// // const rightInOrderLength = inOrder.length - indexIn;
// // const leftIndexPost = postOrder.length - 1 - rightInOrderLength;
// // iI
// // [ 'd', 'b', 'e', 'a', 'f', 'c', 'g' ],
// // iML
// // [ 'd', 'e', 'b', 'f', 'g', 'c', 'a' ]
// const leftInOrder = inOrder.slice(0, indexIn);
// const leftPostOrder = postOrder.slice(0, leftInOrder.length);
// const rightInOrder = inOrder.slice(indexIn + 1);
// const rightPostOrder = postOrder.slice(leftInOrder.length, indexPost);
// // console.log(leftInOrder, leftPostOrder, rightInOrder, rightPostOrder);
// // console.log(root);
// root.left = buildTreeInPost(leftInOrder, leftPostOrder);
// root.right = buildTreeInPost(rightInOrder, rightPostOrder);
// return root;
// };
// console.log(
// buildTreeInPost(
// ["d", "b", "g", "e", "h", "a", "c", "f"],
// ["d", "g", "h", "e", "b", "f", "c", "a"]
// )
// );