当前位置:网站首页>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;
}
边栏推荐
- LeetCode 445. 两数相加 II
- codevs 2370 Small room tree (LCA)
- std::move()
- Network Fundamentals (Section 1)
- How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
- 传三星3nm斩获第二家客户,目前产能已供不应求
- HDU 4135: Co-prime (the principle of inclusion and exclusion)
- CPU多级缓存与缓存一致性
- ENVI 5.3软件安装包和安装教程
- Buckle Exercise - 61 Sort by frequency of characters
猜你喜欢
Network sockets (UDP and TCP programming)
振弦传感器及核心VM系列振弦采集模块
基于UiAutomator2+PageObject模式开展APP自动化测试实战
mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
基于UiAutomator2+PageObject模式开展APP自动化测试实战
接口定义与实现
学长告诉我,大厂MySQL都是通过SSH连接的
模块九 - 设计电商秒杀系统
A little self-deprecating deconstruction about farmers "code"
随机推荐
Since the media hot style title how to write?Taught you how to write the title
网络基础(第一节)
leetcode 823. Binary Trees With Factors(因子二叉树)
负载均衡原理分析与源码解读
LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
If someone asks you about distributed transactions again, throw this to him
Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
电脑怎么设置屏幕息屏时间(日常使用分享)
codevs 2370 Small room tree (LCA)
StoneDB 文档捉虫活动第一季
Centos7 environment uses Mysql offline installation package to install Mysql5.7
【Redis】内存回收策略
Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
接口定义与实现
振弦传感器及核心VM系列振弦采集模块
关于振弦采集模块及采集仪振弦频率值准确率的问题
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
LeetCode 86. 分隔链表
面试官:你们是如何保证接口的幂等性?
基于UiAutomator2+PageObject模式开展APP自动化测试实战