-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path108.cpp
More file actions
26 lines (25 loc) · 670 Bytes
/
Copy path108.cpp
File metadata and controls
26 lines (25 loc) · 670 Bytes
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
// recursion.cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
TreeNode* recursion(int low, int high, vector<int>& nums) {
if (low >= high)
return nullptr;
int mid = (low + high) / 2;
TreeNode* p = new TreeNode(nums[mid]);
p->left = recursion(low, mid, nums);
p->right = recursion(mid + 1, high, nums);
return p;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return recursion(0, nums.size(), nums);
}
};