Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Subscribe to see which companies asked this question
要点就是找到中心点,然后分别递归构造左边的数和右边的数
TreeNode* sortedArrayToBST(vector<int>& nums, int beg, int end) {
if (beg > end)
return nullptr;
int mid = (beg + end) >> ;
TreeNode* root = new TreeNode(nums[mid]);
root->left = sortedArrayToBST(nums, beg, mid - );
root->right = sortedArrayToBST(nums, mid + , end);
return root; } TreeNode* sortedArrayToBST(vector<int>& nums) {
return sortedArrayToBST(nums, , nums.size()-);
}
链表为了代码的简洁,我使用了二级指针,不过可读性变差了
TreeNode* sortedListToBST(ListNode* head) {
if (head == nullptr)
return nullptr;
ListNode* fast = head;
ListNode** slow = &head;
while (fast->next != nullptr && fast->next->next != nullptr)
{
fast = fast->next->next;
slow = &((*slow)->next);
}
TreeNode* root = new TreeNode((*slow)->val); root->right = sortedListToBST((*slow)->next);
*slow = nullptr;
root->left = sortedListToBST(head); return root;
}
Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree的更多相关文章
- [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode Find Minimum in Rotated Sorted Array
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array (3 solutions)
Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- LeetCode--Array--Remove Duplicates from Sorted Array (Easy)
26. Remove Duplicates from Sorted Array (Easy) Given a sorted array nums, remove the duplicates in-p ...
随机推荐
- Everything开机自启
第一步:关闭自动启动 在设置中关闭everything开机自动启动. 第二步:创建计划任务 点击“开始”—“任务计划程序”—“创建任务” 首先设置任务的名称等,注意一定要勾选“使用最高权限运行”. 然 ...
- 上传预览 easyui部分控件获取focuse 表单验证
js: $(document).ready(function () { //$('#creater').combobox({ // url: '/VMS.UI/BindData/ScheamData? ...
- depcomp: line 571: exec: g++: not found
../depcomp: line 571: exec: g++: not foundmake[1]: *** [my_new.o] Error 127make[1]: Leaving director ...
- line-height系列(二)——对行内元素(文字、图片、兄弟元素)、块级元素设置line-height后的表现
>原创文章,转载请注明来源! 二.对行内元素(文字.图片.兄弟元素).块级元素设置line-height后的表现 对块级元素无效,对行内元素有效.可继承给行内元素. 文字的line-height ...
- HDU5882
Balanced Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Java错题
加粗为正确答案,绿色为错选答案 1.对于以下代码: for ( int i=0; i<10; i++) System.out.println(i); for循环后,i的值是多少? A.i不再存 ...
- Div.2 C. Dasha and Password
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- [css]《css揭秘》学习(四)-一个元素实现内圆角边框
如图所示的圆角边框有两个元素很好实现,用一个元素也可以实现. <html> <head> <meta charset="utf-8"> < ...
- C++编程练习(17)----“二叉树非递归遍历的实现“
二叉树的非递归遍历 最近看书上说道要掌握二叉树遍历的6种编写方式,之前只用递归方式编写过,这次就用非递归方式编写试一试. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历 ...
- C++编程练习(7)----“KMP模式匹配算法“字符串匹配
子串在主串中的定位操作通常称做串的模式匹配. KMP模式匹配算法实现: /* Index_KMP.h头文件 */ #include<string> #include<sstream& ...