LeetCode C++ 109. Convert Sorted List to Binary Search Tree【DFS/Linked List】中等
发布日期:2021-07-01 02:49:53
浏览次数:2
分类:技术文章
本文共 2300 字,大约阅读时间需要 7 分钟。
Given the head
of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1
.
Example 1:
Input: head = [-10,-3,0,5,9]Output: [0,-3,9,-10,null,5]Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
Example 2:
Input: head = []Output: []
Example 3:
Input: head = [0]Output: [0]
Example 4:
Input: head = [1,3]Output: [3,1]
Constraints:
- The numner of nodes in
head
is in the range[0, 2 * 10^4]
. -10^5 <= Node.val <= 10^5
题意:给出一个升序有序的单链表,将其转换成高度平衡的二叉搜索树。
思路:双指针+递归。两个指针,一快一慢,快的每次走两步,慢的每次走一步,当快指针遍历结束时,慢指针指向的也就是链表的中间位置,将其作为二叉搜索树当前结点的值。然后递归左右链表形成左右子树。注意:需要断开左右链表!
代码:
class Solution { public: TreeNode* sortedListToBST(ListNode* head) { if(!head) return nullptr; if(!head->next) return new TreeNode(head->val); //找到链表的中点slow ListNode *slow = head, fast = head, prev = head; while(fast && fast->next){ fast = fast->next->next; slow = slow->next; } //prev指向中间位置的前一个结点 while(prev->next != slow) prev = prev->next; //将中点左边的链表分开 prev->next = nullptr; //递归建立子树 root = new TreeNode(slow->val); root->left = sortedListToBST(head); root->right = sortedListToBST(slow->next); return root; } };
如果将寻找中点和前一个结点的过程结合起来,代码如下:
class Solution { public: TreeNode* sortedListToBST(ListNode* head) { if (head == nullptr) return nullptr; if (head->next == nullptr) return new TreeNode(head->val); //leftEnd指向mid的前一个结点, mid指向第二个结点 //按照快指针的走法, midNext此时应到第三个结点 ListNode *leftEnd = head, *mid = head->next, *midNext = mid->next; while (midNext && midNext->next) { leftEnd = leftEnd->next; mid = mid->next; midNext = midNext->next->next; } leftEnd->next = nullptr; TreeNode *root = new TreeNode(mid->val); root->left = sortedListToBST(head); root->right = sortedListToBST(mid->next); return root; }};
效率:
执行用时:28 ms, 在所有 C++ 提交中击败了99.82% 的用户内存消耗:24.1 MB, 在所有 C++ 提交中击败了100.00% 的用户
转载地址:https://memcpy0.blog.csdn.net/article/details/108112457 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月21日 06时11分07秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
JAVA学习笔记6 - 数组
2019-04-30
【学习笔记】Android Activity
2019-04-30
location区段
2019-04-30
linux内存的寻址方式
2019-04-30
how2heap-double free
2019-04-30
tf keras SimpleRNN源码解析
2019-04-30
MyBatisPlus简单入门(SpringBoot)
2019-04-30
xss-labs详解(上)1-10
2019-04-30
xss-labs详解(下)11-20
2019-04-30
攻防世界web进阶区ics-04详解
2019-04-30
Linux png转jpg (convert命令)
2019-04-30
CodeForces - 456C Boredom (dp)
2019-04-30
CodeForces - 1042B Vitamins (思维)
2019-04-30
ACM 2013 长沙区域赛 Collision (几何)
2019-04-30
ACM 2014 鞍山区域赛 E - Hatsune Miku (dp)
2019-04-30
反向传播&梯度下降 的直观理解程序(numpy)
2019-04-30
ACM 2017 北京区域赛 J-Pangu and Stones(区间dp)
2019-04-30
java常用类 String面试题
2019-04-30
四线触摸屏原理
2019-04-30