We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 98b6666 commit bf2314aCopy full SHA for bf2314a
c/108-convert-sorted-array.c
@@ -0,0 +1,22 @@
1
+/*
2
+Given an integer array nums where the elements are sorted in
3
+ascending order, convert it to a height-balanced binary search tree.
4
+
5
+Space: O(n)
6
+Time: O(n)
7
+*/
8
9
+struct TreeNode* dichomoty_rec(int* nums, int i, int j) {
10
+ if (i>j)
11
+ return NULL;
12
+ struct TreeNode* new_t = malloc(sizeof(struct TreeNode));
13
+ int m = (i+j)/2;
14
+ new_t->val = nums[m];
15
+ new_t->left = dichomoty_rec(nums, i, m-1);
16
+ new_t->right = dichomoty_rec(nums, m+1, j);
17
+ return new_t;
18
+}
19
20
+struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
21
+ return dichomoty_rec(nums, 0, numsSize-1);
22
0 commit comments