Welcome to Subscribe On Youtube
105. Construct Binary Tree from Preorder and Inorder Traversal
Description
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.
Solutions
Solution 1: Recursion
The first node $preorder[0]$ in the preorder sequence is the root node. We find the position $i$ of the root node in the inorder sequence, which divides the inorder sequence into the left subtree $inorder[0..i]$ and the right subtree $inorder[i+1..]$.
Through the intervals of the left and right subtrees, we can calculate the number of nodes in the left and right subtrees, assumed to be $m$ and $n$ respectively. Then in the preorder nodes, the $m$ nodes following the root node are the left subtree, and the $n$ nodes after that are the right subtree.
We can solve this recursively.
Preorder traversal: traverse the root node first, then traverse the left and right subtrees; Inorder traversal: traverse the left subtree first, then traverse the root node, and finally traverse the right subtree.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
If the node values given in the problem have duplicates, then we only need to record all the positions where each node value appears, and then recursively construct the tree.
-
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { private int[] preorder; private Map<Integer, Integer> d = new HashMap<>(); public TreeNode buildTree(int[] preorder, int[] inorder) { int n = preorder.length; this.preorder = preorder; for (int i = 0; i < n; ++i) { d.put(inorder[i], i); } return dfs(0, 0, n); } private TreeNode dfs(int i, int j, int n) { if (n <= 0) { return null; } int v = preorder[i]; int k = d.get(v); TreeNode l = dfs(i + 1, j, k - j); TreeNode r = dfs(i + 1 + k - j, k + 1, n - 1 - (k - j)); return new TreeNode(v, l, r); } }
-
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { int n = preorder.size(); unordered_map<int, int> d; for (int i = 0; i < n; ++i) { d[inorder[i]] = i; } function<TreeNode*(int, int, int)> dfs = [&](int i, int j, int n) -> TreeNode* { if (n <= 0) { return nullptr; } int v = preorder[i]; int k = d[v]; TreeNode* l = dfs(i + 1, j, k - j); TreeNode* r = dfs(i + 1 + k - j, k + 1, n - 1 - (k - j)); return new TreeNode(v, l, r); }; return dfs(0, 0, n); } };
-
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right # best class Solution: def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: if not preorder: # or, not inorder return None v = preorder[0] i = inorder.index(v) root = TreeNode(v) root.left = self.buildTree(preorder[1: i+1], inorder[:i]) root.right = self.buildTree(preorder[i+1:], inorder[i+1:]) return root # inorder.index(preorder_val) class Solution: def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: def dfs(pleft, pright, ileft, iright): if pleft > pright or ileft > iright: return None k = inorder.index(preorder[pleft]) root = TreeNode(preorder[pleft]) root.left = dfs(pleft + 1, pleft + (k - ileft), ileft, k - 1) root.right = dfs(pleft + 1 + (k - ileft), pright, k + 1, iright) return root n = len(preorder) return dfs(0, n - 1, 0, n - 1) # build dict for val => index class Solution: def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: def dfs(pleft, pright, ileft, iright): if pleft > pright or ileft > iright: return None v = preorder[pleft] k = d[v] root = TreeNode(v) root.left = dfs(pleft + 1, pleft + (k - ileft), ileft, k - 1) root.right = dfs(pleft + 1 + (k - ileft), pright, k + 1, iright) return root d = {v: i for i, v in enumerate(inorder)} n = len(preorder) return dfs(0, n - 1, 0, n - 1) ############ ''' search for index, also use .index(val) >>> a = [1,2,3,4,5] >>> a.index(3) 2 ''' class Solution(object): def buildTree(self, preorder, inorder): """ :type preorder: List[int] :type inorder: List[int] :rtype: TreeNode """ self.preindex = 0 ind = {v: i for i, v in enumerate(inorder)} head = self.dc(0, len(preorder) - 1, preorder, inorder, ind) return head def dc(self, start, end, preorder, inorder, ind): if start <= end: mid = ind[preorder[self.preindex]] self.preindex += 1 root = TreeNode(inorder[mid]) root.left = self.dc(start, mid - 1, preorder, inorder, ind) root.right = self.dc(mid + 1, end, preorder, inorder, ind) return root ############ # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: def dfs(i: int, j: int, n: int): if n <= 0: return None v = preorder[i] k = d[v] l = dfs(i + 1, j, k - j) r = dfs(i + 1 + k - j, k + 1, n - k + j - 1) return TreeNode(v, l, r) d = {v: i for i, v in enumerate(inorder)} return dfs(0, 0, len(preorder))
-
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func buildTree(preorder []int, inorder []int) *TreeNode { d := map[int]int{} for i, x := range inorder { d[x] = i } var dfs func(i, j, n int) *TreeNode dfs = func(i, j, n int) *TreeNode { if n <= 0 { return nil } v := preorder[i] k := d[v] l := dfs(i+1, j, k-j) r := dfs(i+1+k-j, k+1, n-1-(k-j)) return &TreeNode{v, l, r} } return dfs(0, 0, len(preorder)) }
-
/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ function buildTree(preorder: number[], inorder: number[]): TreeNode | null { const d: Map<number, number> = new Map(); const n = inorder.length; for (let i = 0; i < n; ++i) { d.set(inorder[i], i); } const dfs = (i: number, j: number, n: number): TreeNode | null => { if (n <= 0) { return null; } const v = preorder[i]; const k = d.get(v)!; const l = dfs(i + 1, j, k - j); const r = dfs(i + 1 + k - j, k + 1, n - 1 - (k - j)); return new TreeNode(v, l, r); }; return dfs(0, 0, n); }
-
/** * 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) { const d = new Map(); const n = inorder.length; for (let i = 0; i < n; ++i) { d.set(inorder[i], i); } const dfs = (i, j, n) => { if (n <= 0) { return null; } const v = preorder[i]; const k = d.get(v); const l = dfs(i + 1, j, k - j); const r = dfs(i + 1 + k - j, k + 1, n - 1 - (k - j)); return new TreeNode(v, l, r); }; return dfs(0, 0, n); };
-
// Definition for a binary tree node. // #[derive(Debug, PartialEq, Eq)] // pub struct TreeNode { // pub val: i32, // pub left: Option<Rc<RefCell<TreeNode>>>, // pub right: Option<Rc<RefCell<TreeNode>>>, // } // // impl TreeNode { // #[inline] // pub fn new(val: i32) -> Self { // TreeNode { // val, // left: None, // right: None // } // } // } use std::rc::Rc; use std::cell::RefCell; use std::collections::HashMap; impl Solution { pub fn build_tree(preorder: Vec<i32>, inorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> { let mut d = HashMap::new(); for (i, &x) in inorder.iter().enumerate() { d.insert(x, i); } Self::dfs(&preorder, &d, 0, 0, preorder.len()) } pub fn dfs( preorder: &Vec<i32>, d: &HashMap<i32, usize>, i: usize, j: usize, n: usize ) -> Option<Rc<RefCell<TreeNode>>> { if n <= 0 { return None; } let v = preorder[i]; let k = d[&v]; let mut root = TreeNode::new(v); root.left = Self::dfs(preorder, d, i + 1, j, k - j); root.right = Self::dfs(preorder, d, i + k - j + 1, k + 1, n - k + j - 1); Some(Rc::new(RefCell::new(root))) } }