You are given two integer arrays, preorder and inorder.
Your task is to construct and return the binary tree that would result from these traversals.
You may assume that:
preorder traversal visits the root node first, then recursively traverses the left subtree, and finally the right subtree.inorder traversal recursively visits the left subtree, then the root node, and finally the right subtree.Return the root of the constructed binary tree.
preorder = [3, 9, 20, 15, 7]
inorder = [9, 3, 15, 20, 7]
From preorder [3,9,20,15,7], 3 is the root. In inorder [9,3,15,20,7], 9 is left of 3, and [15,20,7] are right of 3. Recursively build the left and right subtrees.
preorder = [-1]
inorder = [-1]
A single node tree where both traversals consist of just that node.
preorder = []
inorder = []
None
An empty tree results in empty preorder and inorder traversals.
preorder = [3, 9, 20, 15, 7]
inorder = [9, 3, 15, 20, 7]