当前位置:网站首页>LeetCode 109. Sorted Linked List Conversion Binary Search Tree
LeetCode 109. Sorted Linked List Conversion Binary Search Tree
2022-08-10 11:57:00 【Water dish pen】
原题网址:https://leetcode.cn/problems/convert-sorted-list-to-binary-search-tree/
The ordered linked list is transformed into a highly balanced binary search tree.
有序,高度平衡,So it is divided from the middle of the linked list,形成树,The idea of merging is used
// It has to be a highly balanced binary search tree.The seats must be separated by the midpoint of the linked list.
// 由于是有序的,So traverse the linked list,After adding one by one, it is still a linked list structure,不行.
public TreeNode sortedListToBST(ListNode head) {
return mergeListToBST(head,null);
}
// Note that the interval here is left closed and right open
private TreeNode mergeListToBST(ListNode head, ListNode tail) {
// 遍历完了链表,因为是左闭右开的区间
if(head == tail) {
return null;
}
// This is only one element
if(head.next == tail) {
return new TreeNode(head.val);
}
ListNode midNode = getMidNode(head,tail);
TreeNode root = new TreeNode(midNode.val);
root.left = mergeListToBST(head,midNode);
root.right = mergeListToBST(midNode.next,tail);
return root;
}
private ListNode getMidNode(ListNode head, ListNode tail) {
ListNode fast = head;
ListNode slow = head;
while(fast != tail && fast.next != tail) {
fast= fast.next.next;
slow= slow.next;
}
return slow;
}
边栏推荐
- Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
- jlink and swd interface definition
- 托米的咒语
- [E-commerce operation] Do you really understand social media marketing (SMM)?
- If someone asks you about distributed transactions again, throw this to him
- 传三星3nm斩获第二家客户,目前产能已供不应求
- LeetCode 83. 删除排序链表中的重复元素
- 如何使用工程仪器设备在线监测管理系统
- Licking Exercise - 58 Verifying Binary Search Trees
- leetcode 823. Binary Trees With Factors(因子二叉树)
猜你喜欢

WeChat applet, global variables change in one place and the state in other places also changes.

一文读懂NFT数字藏品为何风靡全球?

模块九 - 设计电商秒杀系统

VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions

做自媒体月入几万?博主们都在用的几个自媒体工具
![[E-commerce operation] Do you really understand social media marketing (SMM)?](/img/5b/6682c613305deb3dc15401077d38a0.png)
[E-commerce operation] Do you really understand social media marketing (SMM)?

StoneDB 文档捉虫活动第一季
![[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists](/img/06/9d49fc99ab684f03740deb2abc38e2.png)
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists

零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?

自媒体爆款标题怎么写?手把手教你写热门标题
随机推荐
Analysis of the implementation principle of UUID from the perspective of source code
Pulling drills - 56 Finding the right interval
基于UiAutomator2+PageObject模式开展APP自动化测试实战
StoneDB 文档捉虫活动第一季
LeetCode 19. Delete the Nth last node of the linked list
The brave rice rice, does not fear the brush list of 】 list has a ring
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
什么是幂等性?四种接口幂等性方案详解!
再有人问你分布式事务,把这篇扔给他
Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
力扣练习——63 找到字符串中所有字母异位词
Centos7环境使用Mysql离线安装包安装Mysql5.7
LCD驱动端与设备端名称匹配过程分析(Tiny4412)
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
Nocalhost - 让云原生时代的开发更高效
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
LeetCode 138. Copy a linked list with random pointers
使用哈工大LTP测试分词并且增加自定义字典