[leetcode] Convert Sorted List to Binary Search Tree
来源:互联网 发布:数据分析软件有哪些 编辑:程序博客网 时间:2024/05/29 02:13
Convert Sorted List to Binary Search Tree
解法:
把链表转换为vector之后,解法和上题一样。
class Solution {public: TreeNode *sortedListToBSTbyIndex(vector<int> &num,int begin,int end){ if (begin>end) { return nullptr; } int mid=begin+(end-begin)/2; TreeNode *pleft=sortedListToBSTbyIndex(num, begin, mid-1); TreeNode *pright=sortedListToBSTbyIndex(num, mid+1, end); TreeNode *node=new TreeNode(num[mid]); node->left=pleft; node->right=pright; return node; } TreeNode *sortedListToBST(ListNode *head) { if (head==nullptr) { return nullptr; } vector<int> vt; while (head!=nullptr) { vt.push_back(head->val); head=head->next; } return sortedListToBSTbyIndex(vt,0,vt.size()-1); }};
0 0
- 【LeetCode】Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
- LeetCode[Tree]: Convert Sorted List to Binary Search Tree
- [leetcode][list][dfs] Convert Sorted List to Binary Search Tree
- LeetCode: Convert Sorted List to Binary Search Tree
- [LeetCode]Convert Sorted List to Binary Search Tree
- LeetCode : Convert Sorted List to Binary Search Tree
- [Leetcode] Convert Sorted List to Binary Search Tree
- Leetcode: Convert Sorted List to Balanced Binary Search Tree
- [LeetCode] Convert Sorted List to Binary Search Tree
- leetcode 66: Convert Sorted List to Binary Search Tree
- leetcode Convert Sorted List to Binary Search Tree
- LeetCode Convert Sorted List to Binary Search Tree
- leetcode Convert Sorted List to Binary Search Tree
- LeetCode:Convert Sorted List to Binary Search Tree
- [LeetCode]Convert Sorted List to Binary Search Tree
- [Leetcode]Convert Sorted List to Binary Search Tree
- [leetcode]Convert Sorted List to Binary Search Tree
- Leetcode: Convert Sorted List to Binary Search Tree
- Android深入浅出之Binder机制
- android获取wifi外网ip的方法
- 云游戏
- uva 1252 Twenty Questions 状压dp
- 暑假第十二天,7月18号
- [leetcode] Convert Sorted List to Binary Search Tree
- Python笔记1--关于序列的学习
- 耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3...。凡是报到“3”就退出圈子,最后留在圈子内的人就是出卖耶稣的叛徒。
- 浅尝CKRule规则引擎
- 大牛blog汇总
- 2-1000内的所有完数
- 策略模式
- WAS7升级后遗症
- 将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。