1008. Construct Binary Search Tree from Preorder Traversal**

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());
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值