Convert Sorted Array to Binary Search Tree

Tags : leetcode, tree, bst, array, cpp, easy

Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.

Examples #

Example 1:

Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null,9] is also accepted:

Example 2:

Input: nums = [1,3]
Output: [3,1]
Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.

Constraints #

Solutions #


/**
 * 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* convertintoBST(vector<int> &nums,int left,int right) {
        if(left > right) return NULL;
        int mid = left+(right-left)/2;
        TreeNode *node = new TreeNode(nums[mid]);
        node->left = convertintoBST(nums,left,mid-1);
        node->right = convertintoBST(nums,mid+1,right);
        return node;
    }
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        if(nums.size() == 0) return NULL;
        return convertintoBST(nums,0,nums.size()-1);
    }
};