Skip to content

Commit

Permalink
sortedArrayToBST.txt added
Browse files Browse the repository at this point in the history
  • Loading branch information
kokodeshinu committed Oct 5, 2022
1 parent fec7629 commit d2949d1
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions LeetCode Solutions/sortedArrayToBST.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Problem Link - https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/
/**
* 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* h(vector<int>& nums,int s,int e){
if(s>e){
return NULL;
}
int mid=(s+e)/2;
TreeNode* root=new TreeNode(nums[mid]);
root->left=h(nums,s,mid-1);
root->right=h(nums,mid+1,e);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
int s=0;
int e=nums.size()-1;
return h(nums,s,e);
    }
};

0 comments on commit d2949d1

Please sign in to comment.