Skip to content

Commit 577cf1c

Browse files
committed
🚧 [105] a1
1 parent ab0c43e commit 577cf1c

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

105/my_solution.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class TreeNode {
2+
constructor(val, left, right) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.left = (left === undefined ? null : left);
5+
this.right = (right === undefined ? null : right);
6+
}
7+
}
8+
9+
/**
10+
* @param {number[]} preorder
11+
* @param {number[]} inorder
12+
* @return {TreeNode}
13+
*/
14+
var buildTree = function (preorder, inorder) {
15+
let tree = new TreeNode()
16+
17+
const buildNode = (node, order) => {
18+
// if (null === node) return;
19+
if (0 === order.length) return;
20+
let curr = order[0],
21+
tmpInorder = [...inorder],
22+
inIdx = tmpInorder.indexOf(curr),
23+
leftInorder = tmpInorder.splice(0, inIdx),
24+
rightInorder = tmpInorder.splice(inIdx, tmpInorder.length - 1);
25+
26+
console.log(`-> inIdx:${inIdx}, curr:${curr}, order:${order}`);
27+
console.log("from inorder:");
28+
console.log(leftInorder, rightInorder);
29+
30+
// last idx from inorder of the left side is the end of the left leaves in pre
31+
let tmpPreorder = [...order],
32+
lastPreIdx = order.indexOf(leftInorder[leftInorder.length - 1]),
33+
leftPreorder = tmpPreorder.splice(1, lastPreIdx),
34+
rightPreorder = tmpPreorder.splice(lastPreIdx, tmpPreorder.length - 1);
35+
console.log(">> PRE")
36+
console.log(leftPreorder, rightPreorder)
37+
38+
node.val = curr;
39+
console.log("node")
40+
console.log(node)
41+
42+
43+
console.log(order);
44+
// if (inorder.indexOf(preorder[0]) < inIdx) {
45+
// console.log(`-> l ${inorder.indexOf(preorder[0])} < ${inIdx}`);
46+
if (leftPreorder.length > 0) {
47+
// // yes it is left leave
48+
node.left = new TreeNode(order[1]);
49+
console.log(`Adding ${order[1]} to left`)
50+
buildNode(node.left, leftPreorder);
51+
}
52+
53+
if (rightPreorder.length > 0) {
54+
// console.log(`-> r ${inorder.indexOf(preorder[0])} > ${inIdx}`);
55+
console.log(`Adding ${order[lastPreIdx + 1]} to right`)
56+
node.right = new TreeNode(order[lastPreIdx + 1]);
57+
buildNode(node.right, rightPreorder);
58+
}
59+
}
60+
61+
buildNode(tree, [...preorder]);
62+
63+
return tree;
64+
};
65+
66+
let x =
67+
// buildTree([3, 9, 20, 15, 7], [9, 3, 15, 20, 7])
68+
// buildTree([-1], [-1])
69+
buildTree([1, 2, 3], [3, 2, 1])
70+
// buildTree([3, 9, 8, 6, 7, 20], [6, 8, 9, 7, 3, 20])
71+
72+
console.log("Result")
73+
console.log(x);

105/solution.js

Whitespace-only changes.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- [100. Same Tree](./100/)
4040
- [102. Binary Tree Level Order Traversal](./102/)
4141
- [104. Maximum Depth of Binary Tree](./104/)
42+
- [105. Construct Binary Tree from Preorder and Inorder Traversal](./105/)
4243
- [121. Best Time to Buy and Sell Stock](./121/)
4344
- [128. Longest Consecutive Sequence](./128/)
4445
- [133. Clone Graph](./133/)
@@ -142,7 +143,7 @@ Batch create:
142143
NOTE: JS IS HERE
143144
-->
144145
```ssh
145-
chapter=102 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
146+
chapter=105 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
146147
```
147148
> then you can use `x` for quick debug.
148149

0 commit comments

Comments
 (0)