105. Construct Binary Tree from Preorder and Inorder Traversal
Problem
Tags: Array
, Hash Table
, Divide and Conquer
, Tree
, Binary Tree
Given two integer arrays preorder
and inorder
where preorder
is the preorder traversal of a binary tree and inorder
is the inorder traversal of the same tree, construct and return the binary tree.
Example 1:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Constraints:
1 <= preorder.length <= 3000
inorder.length == preorder.length
-3000 <= preorder[i], inorder[i] <= 3000
preorder
andinorder
consist of unique values.- Each value of
inorder
also appears inpreorder
. preorder
is guaranteed to be the preorder traversal of the tree.inorder
is guaranteed to be the inorder traversal of the tree.
Code
JS
// 105. Construct Binary Tree from Preorder and Inorder Traversal (6/15/53405)
// Runtime: 100 ms (72.57%) Memory: 41.86 MB (94.44%)
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function(preorder, inorder) {
// 以便從節點 value 來反查在 inorder 中的位置
let inorder_location = new Map();
for (let i = 0; i < inorder.length; i++) inorder_location.set(inorder[i], i);
function tree_build(preorder_root_location, inorder_left_bound, inorder_right_bound) {
// 建構 root 節點,獲取 root 在 inorder 的位置
let root_value = preorder[preorder_root_location],
root = new TreeNode(root_value),
inorder_root_location = inorder_location.get(root_value);
// 以左子節點為 root 建構子樹
if (inorder_root_location > inorder_left_bound)
root.left = tree_build(preorder_root_location + 1, inorder_left_bound, inorder_root_location - 1)
// 以右子節點為 root 建構子樹
if (inorder_root_location < inorder_right_bound)
root.right = tree_build(preorder_root_location + inorder_root_location - inorder_left_bound + 1, inorder_root_location + 1, inorder_right_bound)
// 回傳 root,包含其子樹
return root;
}
// 開始建構這棵樹,從 root (preorder[0]) 開始做,inorder 的範圍為整個的 inorder
return tree_build(0, 0, inorder.length - 1);
};