leetcode刷题,总结,记录,备忘109
来源:互联网 发布:网络麻将 编辑:程序博客网 时间:2024/06/09 16:57
leetcode109
Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Subscribe to see which companies asked this question
使用二分法,然后分别分为2个分支,进行递归操作,还是比较简单的,但是貌似算法耗时有点多,40ms,,但是在leetcode提交通过的解决方案中排的比较靠后。。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode * result; void funciton(vector<int> vi, TreeNode * root) { if (root == NULL) { root = new TreeNode(0); result = root; } if (vi.size() == 1) { root->val = vi[0]; root->left = NULL; root->right = NULL; } else if (vi.size() == 2) { root->val = vi[0]; root->left = NULL; root->right = new TreeNode(vi[1]); } else { int mid = vi.size() / 2; root->val = vi[mid]; root->left = new TreeNode(0); root->right = new TreeNode(0); vector<int> vl(vi.begin(), vi.begin() + mid); vector<int> vr(vi.begin() + mid + 1, vi.end()); funciton(vl, root->left); funciton(vr, root->right); } } TreeNode* sortedListToBST(ListNode* head) { vector<int> vi; if (head == NULL) { return NULL; } while (head) { vi.push_back(head->val); head = head->next; } funciton(vi, NULL); return result; }};然后在讨论区看到一个解法,受到的启发,还记得leetcode之前有个判断1个链表是否是循环的题,使用快慢指针,这个方法就是使用快慢指针,找到中间的指针,然后也是1分为2,递归做二分法,比较巧妙的做法,感觉比我自己想 的解法更好,下面也把这个解决方法贴出来。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* sortedListToBST(ListNode* head) { if (head == NULL) { return NULL; } ListNode * fast = head; ListNode * slow = head; ListNode * pre = NULL; while (fast && fast->next) { fast = fast->next->next; pre = slow; slow = slow->next; } if (pre) { pre->next = NULL; } else { head = NULL; } TreeNode * root = new TreeNode(slow->val); root->left = sortedListToBST(head); root->right = sortedListToBST(slow->next); return root; }};
0 0
- leetcode刷题,总结,记录,备忘109
- leetcode刷题。总结,记录,备忘 104
- leetcode刷题。总结,记录,备忘 100
- leetcode刷题。总结,记录,备忘 191
- leetcode刷题。总结,记录,备忘。168
- leetcode刷题, 总结,记录,备忘 171
- leetcode刷题 ,总结,记录,备忘。144
- leetcode刷题,总结,记录, 备忘 141
- leetcode刷题,总结,记录,备忘。116
- leetcode刷题,总结,记录,备忘。96
- leetcode刷题。记录,总结,备忘。217
- leetcode 刷题, 总结,记录,备忘 35
- leetcode刷题,总结,记录,备忘 226
- leetcode刷题,总结,记录,备忘 169
- leetcode刷题,总结,记录,备忘137
- leetcode刷题,总结,记录,备忘83
- leetcode刷题,总结,记录,备忘70
- leetcode刷题,总结,记录,备忘 108
- 设置系统工作在SVC模式---嵌入式回归第七篇
- bootloader_v2 以及改进PatchDSE!(静态过win8.1驱动签名限制)
- 记录一下从开始做Android app整个过程中遇到的问题
- 第一次月赛题目—兔子与樱花(单源最短路。最后一分钟ac的,想想都有点小激动啊)
- 画蛇添足的Mac Navicat Premium中文数据乱码
- leetcode刷题,总结,记录,备忘109
- Jenkins简单使用介绍
- ModifyStyle函数的用法
- 26.订单管理
- mysql表的字符集uft8转换成gbk测试
- Android的Drawable之BitmapDrawable
- 树的子结构[数据结构]
- 二叉树遍历
- 面试题大集合