-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path106.js
More file actions
30 lines (26 loc) · 916 Bytes
/
106.js
File metadata and controls
30 lines (26 loc) · 916 Bytes
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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} inorder
* @param {number[]} postorder
* @return {TreeNode}
*/
var buildTree = function(inorder, postorder) {
if (inorder.length === 0) return null;
if (inorder.length === 1) return new TreeNode(inorder[0]);
const mid = postorder[postorder.length - 1];
const index = inorder.indexOf(mid);
const inorderLeft = inorder.slice(0, index);
const inorderRight = inorder.slice(index + 1, inorder.length);
const postorderLeft = postorder.slice(0, inorderLeft.length);
const postorderRight = postorder.slice(inorderLeft.length, postorder.length - 1);
const returnValue = new TreeNode(mid);
returnValue.left = buildTree(inorderLeft, postorderLeft);
returnValue.right = buildTree(inorderRight, postorderRight);
return returnValue;
};