[LeetCode] 109. 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.
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:
Given the sorted linked list: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: 0 / \ -3 9 / / -10 5
和 108. Convert Sorted Array to Binary Search Tree 思路一样,只不过一个是数组,一个是链表。数组可以通过index直接访问元素找到中点,而链表的查找中间点要通过快慢指针来操作。
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Solution { public TreeNode sortedListToBST(ListNode head) { if (head== null ) return null ; return toBST(head, null ); } public TreeNode toBST(ListNode head, ListNode tail){ ListNode slow = head; ListNode fast = head; if (head==tail) return null ; while (fast!=tail&&fast.next!=tail){ fast = fast.next.next; slow = slow.next; } TreeNode thead = new TreeNode(slow.val); thead.left = toBST(head,slow); thead.right = toBST(slow.next,tail); return thead; } } |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class ListNode: def __init__( self , x): self .val = x self . next = None class Solution: head = None # @param head, a list node # @return a tree node def sortedListToBST( self , head): current, length = head, 0 while current is not None : current, length = current. next , length + 1 self .head = head return self .sortedListToBSTRecu( 0 , length) def sortedListToBSTRecu( self , start, end): if start = = end: return None mid = start + (end - start) / 2 left = self .sortedListToBSTRecu(start, mid) current = TreeNode( self .head.val) current.left = left self .head = self .head. next current.right = self .sortedListToBSTRecu(mid + 1 , end) return current |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | def sortedListToBST( self , head): if not head: return if not head. next : return TreeNode(head.val) slow, fast = head, head. next . next while fast and fast. next : fast = fast. next . next slow = slow. next tmp = slow. next slow. next = None root = TreeNode(tmp.val) root.left = self .sortedListToBST(head) root.right = self .sortedListToBST(tmp. next ) return root |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class Solution { public : TreeNode* sortedListToBST(ListNode* head) { auto curr = head; int n = 0; while (curr) { curr = curr->next; ++n; } return BuildBSTFromSortedDoublyListHelper(&head, 0, n); } TreeNode * BuildBSTFromSortedDoublyListHelper(ListNode **head, int s, int e) { if (s == e) { return nullptr ; } int m = s + ((e - s) / 2); auto left = BuildBSTFromSortedDoublyListHelper(head, s, m); auto curr = new TreeNode((*head)->val); *head = (*head)->next; curr->left = left; curr->right = BuildBSTFromSortedDoublyListHelper(head, m + 1, e); return curr; } }; |
类似题目:
[LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构