/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode *sortedArrayToBST(vector<int> &nums) {
if (nums.empty())
return nullptr;
auto *root = new TreeNode(nums[nums.size() / 2]);
vector<int> left(nums.begin(), nums.begin() + nums.size() / 2);
root->left = sortedArrayToBST(left);
vector<int> right(nums.begin() + nums.size() / 2 + 1, nums.end());
root->right = sortedArrayToBST(right);
return root;
}
面试题 04.02. 最小高度树
最新推荐文章于 2020-08-17 08:35:13 发布