-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path108.convert-sorted-array-to-binary-search-tree.cpp
More file actions
79 lines (78 loc) · 1.79 KB
/
Copy path108.convert-sorted-array-to-binary-search-tree.cpp
File metadata and controls
79 lines (78 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @lc app=leetcode id=108 lang=cpp
*
* [108] Convert Sorted Array to Binary Search Tree
*
* https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/
*
* algorithms
* Easy (66.99%)
* Likes: 7738
* Dislikes: 393
* Total Accepted: 830.3K
* Total Submissions: 1.2M
* Testcase Example: '[-10,-3,0,5,9]'
*
* Given an integer array nums where the elements are sorted in ascending
* order, convert it to a height-balanced binary search tree.
*
* A height-balanced binary tree is a binary tree in which the depth of the two
* subtrees of every node never differs by more than one.
*
*
* 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:
*
*
* 1 <= nums.length <= 10^4
* -10^4 <= nums[i] <= 10^4
* nums is sorted in a strictly increasing order.
*
*
*/
// @lc code=start
/**
* 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 *A(vector<int> &nums, int l, int r)
{
if (l > r)
return NULL;
int mid = (r + l) / 2;
TreeNode *root = new TreeNode(nums[mid], A(nums, l, mid - 1), A(nums, mid + 1, r));
return root;
}
TreeNode *sortedArrayToBST(vector<int> &nums)
{
return A(nums, 0, nums.size() - 1);
}
};
// @lc code=end