[LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
2024-10-19 23:26:51
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:
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:
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:
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++:
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 把有序数组转成二叉搜索树
All LeetCode Questions List 题目汇总
最新文章
- R语言实战(一)介绍、数据集与图形初阶
- bzoj1012
- SqlServer中计算列详解
- Mac下显示隐藏文件 以及修改 hosts文件内容
- 获取XMLHttpRequest对象
- ViewPagerIndicator的使用方法
- Windows下Python读取GRIB数据
- 可视化Git版本管理工具SourceTree的使用
- 201521123109《java程序设计》第六周学习总结
- js回到顶部------转载
- idea操作整理
- centos7下kubernetes(6。运行应用)
- 通过jquery 获取用户当前所在的城市名称和IP地址
- 【Python】【辅助程序】练手小程序:记录外网动态IP地址
- C++中的智能指针
- 1分钟试用PowerShell 5.0新功能PowerShellGet安装Script Browser和Script Analyzer
- CH 0101 - a^b / CH 0102 - 64位整数乘法 - [快速幂和快速乘]
- hihoCoder week7 完全背包
- postgresql-日志表
- Execute Javascript in iOS Applications