LeetCode #: 1008
Difficulty: Medium
Topics: Tree.
Return the root node of a binary search tree that matches the given preorder
traversal.
(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left
has a value <
node.val
, and any descendant of node.right
has a value >
node.val
. Also recall that a preorder traversal displays the value of the node
first, then traverses node.left
, then traverses node.right
.)
Example 1:
Input: [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]
Note:
1 <= preorder.length <= 100
- The values of
preorder
are distinct.
This solution is inspired by this post written by lee215.
The main takeaway points in this solutions are:
-
Every node has an upper bound.
Left
node is bounded by the parent node's value.Right
node is bounded by the ancestor's bound.- Using the examples above:
- The nodes
[5, 1, 7]
are all bounded by8
. - The node
1
is bounded by5
. 8
is the root node, but if you think deeper about it, it is bounded byNumber.MAX_SAFE_INTEGER
. i.e. imagine there is a root parent nodeNumber.MAX_SAFE_INTEGER
with left node being8
.- This also means that both
10
and12
nodes, which are alsoright
nodes, are also bounded byNumber.MAX_SAFE_INTEGER
.
- The nodes
-
We use a recursive function together with an outer index variable
i
to traverse and construct the tree. When we create a tree node, we incrementi
to process the next element in thepreorder
array. -
We don't need to care about lower bound. When we construct the tree, we try to create
left
node first. If the condition fails (i.e. current number is greater than the parent node value), then we try to create theright
node which automatically satisfies the condition, hence no lower bound is needed.
Time complexity: O(n). We iterate through each element in preorder
array only once.
Space complexity: O(h) where h is the height of the tree. Space cost is the recursive stack size.