LINTCODE---排序列表转换为二分查找树
来源:互联网 发布:舞祭组 知乎 编辑:程序博客网 时间:2024/06/06 00:37
LINTCODE—排序列表转换为二分查找树
思路:主要就是用快慢指针找出中间点,作为一个伪“顶点”,然后递归调用自身,返回左右节点;
class Solution {public: /* * @param head: The first node of linked list. * @return: a tree node */ TreeNode * sortedListToBST(ListNode * head) { // write your code here if(head == NULL ) return NULL; if(head -> next == NULL) return new TreeNode(head -> val); //快慢指针找到中间点 ListNode *slow = head ,*fast = head ,*temp = head; while(fast != NULL && fast->next != NULL) { temp = slow; slow = slow -> next; fast = fast -> next -> next; } temp -> next = NULL; // 递归调用自身 TreeNode *resTree = new TreeNode(slow -> val); resTree -> left = sortedListToBST(head); resTree -> right = sortedListToBST(slow -> next); return resTree; }};
阅读全文
0 0
- LintCode-排序列表转换为二分查找树
- lintcode-排序列表转换为二分查找树-106
- LintCode:排序列表转换为二分查找树
- lintcode,排序列表转换为二分查找树
- LintCode 106-排序列表转换为二分查找树
- lintcode 排序列表转换为二分查找树(
- LintCode 排序列表转换为二分查找树
- 排序列表转换为二分查找树-LintCode
- 排序列表转换为二分查找树-LintCode
- LINTCODE---排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 题目:排序列表转换为二分查找树
- 排序列表转化为二分查找树
- Java实现-排列列表转换为二分查找树
- 1058. A+B in Hogwarts (20)
- EXE made in 2017.9.15.
- 有赞直接打开优惠券中心提示404的解决办法
- SSM框架中的术语
- 正规方程推导过程
- LINTCODE---排序列表转换为二分查找树
- hdu 6194 string string string 后缀数组+rmq+容斥
- SkipList跳跃表
- ARM汇编指令
- H5~表单
- Redis _ lesson3 redis 常见的5种数据类型 与 常见的Redis-cli 返回值
- Milk Patterns POJ
- 笔试题——同样的字符串存放的位置不同
- Codeforces 851A Arpa and a research in Mexican wave