当前位置: 代码迷 >> 综合 >> 1008. Construct Binary Search Tree from Preorder Traversal**
  详细解决方案

1008. Construct Binary Search Tree from Preorder Traversal**

热度:74   发布时间:2024-01-26 18:37:15.0

1008. Construct Binary Search Tree from Preorder Traversal**

https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/

题目描述

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.

C++ 实现 1

先用数组中的 nums[0] 来构建根节点, 然后用 1 ~ n - 1 范围内的数来构建左右子树, 先要找到第一个大于 num[0] 的数以及它的位置 idx, 用 1 ~ idx 范围内的元素构建左子树, 用 idx ~ n - 1 的元素构建右子树. 这里我查找第一个大于 nums[0] 的方法是二分法, 实际上可以直接遍历数组来查找, 具体可以看 C++ 实现 2C++ 实现 3.

/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
private:int array_sep(const vector<int> &nums, int start, int end) {int i = start + 1, j = end - 1;while (i <= j) {int mid = i + (j - i) / 2;if (nums[mid] <= nums[start]) i = mid + 1;else j = mid - 1;}return i;}TreeNode* construct(const vector<int> &nums, int start, int end) {if (start >= end) return nullptr;auto root = new TreeNode(nums[start]);auto idx = array_sep(nums, start, end);root->left = construct(nums, start + 1, idx);root->right = construct(nums, idx, end);return root;}
public:TreeNode* bstFromPreorder(vector<int>& preorder) {return construct(preorder, 0, preorder.size());}
};

C++ 实现 2

/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:TreeNode* bstFromPreorder(vector<int>& preorder) {if(preorder.empty())return NULL;int val = preorder[0];int i;for(i=1; i<preorder.size(); ++i){if(preorder[i]>val)break;}std::vector<int> left(preorder.begin()+1, preorder.begin()+i), right(preorder.begin()+i, preorder.end());TreeNode* res = new TreeNode(val);res->left = bstFromPreorder(left);res->right = bstFromPreorder(right);return res;}
};

C++ 实现 3

/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:TreeNode* bstFromPreorder(vector<int>& preorder, int left, int right){if(left==right-1) return nullptr;TreeNode* root=new TreeNode(preorder[left+1]);int iroot=left+1;int i;for(i=left+1;i<right;++i){if(preorder[i]>preorder[iroot])break;}root->left=bstFromPreorder(preorder, left+1, i);root->right=bstFromPreorder(preorder, i-1, right);return root;}TreeNode* bstFromPreorder(vector<int>& preorder) {return bstFromPreorder(preorder, -1, preorder.size());}
};
  相关解决方案