[LeetCode]Convert Sorted Array to Binary Search Tree
[cpp]
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* Convert2BST(vector<int>& num, int start, int end)
{
if(start > end)
return NULL;
int mid = (start+end)/2;
TreeNode* pNode = new TreeNode(num[mid]);
pNode->left = Convert2BST(num, start, mid-1);
pNode->right = Convert2BST(num, mid+1, end);
return pNode;
}
TreeNode *sortedArrayToBST(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return Convert2BST(num, 0, num.size()-1);
}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* Convert2BST(vector<int>& num, int start, int end)
{
if(start > end)
return NULL;
int mid = (start+end)/2;
TreeNode* pNode = new TreeNode(num[mid]);
pNode->left = Convert2BST(num, start, mid-1);
pNode->right = Convert2BST(num, mid+1, end);
return pNode;
}
TreeNode *sortedArrayToBST(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return Convert2BST(num, 0, num.size()-1);
}
};
补充:软件开发 , C++ ,