返回与给定前序遍历 preorder
相匹配的二叉搜索树(binary search tree)的根结点。
(回想一下,二叉搜索树是二叉树的一种,其每个节点都满足以下规则,对于 node.left
的任何后代,值总 < node.val
,而 node.right
的任何后代,值总 > node.val
。此外,前序遍历首先显示节点 node
的值,然后遍历 node.left
,接着遍历 node.right
。)
题目保证,对于给定的测试用例,总能找到满足要求的二叉搜索树。
示例:
输入:[8,5,1,7,10,12] 输出:[8,5,10,1,7,null,12]
提示:
1 <= preorder.length <= 100
1 <= preorder[i] <= 10^8
preorder
中的值互不相同
根据二叉搜索树的性质,DFS 构建即可。
# 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 bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
def dfs(preorder):
if not preorder:
return None
root = TreeNode(preorder[0])
left, right = 1, len(preorder)
while left < right:
mid = (left + right) >> 1
if preorder[mid] > preorder[0]:
right = mid
else:
left = mid + 1
root.left = dfs(preorder[1:left])
root.right = dfs(preorder[left:])
return root
return dfs(preorder)
/**
* 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 {
public TreeNode bstFromPreorder(int[] preorder) {
return dfs(preorder, 0, preorder.length - 1);
}
private TreeNode dfs(int[] preorder, int i, int j) {
if (i > j || i >= preorder.length) {
return null;
}
TreeNode root = new TreeNode(preorder[i]);
int left = i + 1, right = j + 1;
while (left < right) {
int mid = (left + right) >> 1;
if (preorder[mid] > preorder[i]) {
right = mid;
} else {
left = mid + 1;
}
}
root.left = dfs(preorder, i + 1, left - 1);
root.right = dfs(preorder, left, j);
return root;
}
}
/**
* 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* bstFromPreorder(vector<int>& preorder) {
return dfs(preorder, 0, preorder.size() - 1);
}
TreeNode* dfs(vector<int>& preorder, int i, int j) {
if (i > j || i >= preorder.size()) return nullptr;
TreeNode* root = new TreeNode(preorder[i]);
int left = i + 1, right = j + 1;
while (left < right)
{
int mid = (left + right) >> 1;
if (preorder[mid] > preorder[i]) right = mid;
else left = mid + 1;
}
root->left = dfs(preorder, i + 1, left - 1);
root->right = dfs(preorder, left, j);
return root;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func bstFromPreorder(preorder []int) *TreeNode {
var dfs func(i, j int) *TreeNode
dfs = func(i, j int) *TreeNode {
if i > j || i >= len(preorder) {
return nil
}
root := &TreeNode{Val: preorder[i]}
left, right := i+1, len(preorder)
for left < right {
mid := (left + right) >> 1
if preorder[mid] > preorder[i] {
right = mid
} else {
left = mid + 1
}
}
root.Left = dfs(i+1, left-1)
root.Right = dfs(left, j)
return root
}
return dfs(0, len(preorder)-1)
}