Convert Sorted Array to Binary Search Tree

博客围绕将升序排序数组转换为高度平衡的BST展开。先明确题目要求,即数组元素升序,转换后的二叉树每个节点的两子树深度差不超1。接着阐述思路,每次取数组中间节点为根节点,左右区间节点分别构成左右子树,递归构建。最后提及代码实现。

1,题目要求

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

在这里插入图片描述

给定一个数组,其中元素按升序排序,将其转换为高度平衡的BST。

对于这个问题,高度平衡的二叉树被定义为二叉树,其中每个节点的两个子树的深度从不相差超过1。

2,题目思路

对于这道题,给定一个数组,将其转化为一个高度平衡的BST。

当我们给定一个排序好的数组后,根据二叉搜索树的特点,很容易将其转换为一个BST,而且每一个数字都可以作为这棵树的根节点,只不过树的形状不同罢了。

如果我们想让构建的树是一颗高度平衡的二叉树,那么,我们就要保证左右子树的高度之差不能超过1,即两颗子树的节点的个数是一样(或者节点数不超过1),而且这个要求是递归的。

因此,在构建这个BST时,我们每次都取当前数组的中间节点作为根节点,并将左区间内的节点所构成的二叉树作为当前节点的左子树,右区间内的的节点所构成的二叉树作为当前节点的右子树,递归地进行寻找即可。

3,代码实现

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
static auto speedup = [](){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        int n = nums.size();
        if(n == 0)
            return nullptr;
        return BSTHelper(nums, 0, n-1);
    }
    TreeNode* BSTHelper(vector<int>& nums, int left, int right){
        if(left > right)
            return nullptr;
        int mid = left + (right - left)/2;
        TreeNode *root = new TreeNode(nums[mid]);
        root->left = BSTHelper(nums, left, mid-1);
        root->right= BSTHelper(nums, mid+1,right);
        
        return root;
    }
};
To convert the given array to a complete BST, we need to perform the following steps: 1. Sort the array in ascending order 2. Construct a complete binary tree using the sorted array 3. Perform inorder traversal of the binary tree and store the elements in the original array in the same order as the traversal Here's the implementation of the to_bst(lst) function in Python: ```python def to_bst(lst): # Sort the input list lst.sort() # Construct a complete binary tree using the sorted list n = len(lst) if n == 0: return lst root = lst[n // 2] left_subtree = to_bst(lst[:n // 2]) right_subtree = to_bst(lst[n // 2 + 1:]) binary_tree = [root] + left_subtree + right_subtree # Perform inorder traversal of the binary tree and store the elements in the original array inorder_traversal(binary_tree, lst, 0) return lst def inorder_traversal(binary_tree, lst, i): # Perform inorder traversal of the binary tree and store the elements in the original array n = len(binary_tree) if i >= n: return inorder_traversal(binary_tree, lst, 2 * i + 1) lst[i] = binary_tree[i] inorder_traversal(binary_tree, lst, 2 * i + 2) ``` The to_bst(lst) function takes in the input list and returns the same list after converting it to a complete BST. The function first sorts the input list in ascending order. It then constructs a complete binary tree using the sorted list by recursively dividing the list into two halves and setting the middle element as the root of the binary tree. Finally, the function performs an inorder traversal of the binary tree and stores the elements in the original list in the same order as the traversal.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值