LeetCode: 109. Convert Sorted List to Binary Search Tree

80块钱的Wi-Fi SIP话机,搞定北京联通固话无线化改造(附ATCOM A10W详细配置) 本文详细介绍了如何用80元的Wi-Fi SIP话机(以ATCOM A10W为例)实现北京联通固话的无线化改造,摆脱电话线束缚。内容包括设备选购、网络环境配置、SIP账户设置及常见问题排查,特别适合家庭和小型办公室用户。通过SIP协议和IMS系统,轻松实现高质量无线通话。 阅读详情

题目

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

解题思路

这道题跟LeetCode: 108. Convert Sorted Array to Binary Search Tree一样,但从array变成list。一个最直观的做法就是先把list转换为array,再调用108的函数就可以啦。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def sortedListToBST(self, head: ListNode) -> TreeNode:
        nums= []
        while head is not None:
            nums.append(head.val)
            head = head.next
        if len(nums)==0:
            return None
        l=int(len(nums)/2)
        root = TreeNode(nums[l])
        root.left = self.sortedArrayToBST(nums[:l])
        root.right = self.sortedArrayToBST(nums[l+1:])
        return root
        
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        if len(nums)==0:
            return None
        l=int(len(nums)/2)
        root = TreeNode(nums[l])
        root.left = self.sortedArrayToBST(nums[:l])
        root.right = self.sortedArrayToBST(nums[l+1:])
        return root

这个做法有点绕,我去看看大佬的做法。
用了快慢指针确定中位数,然后递归调用自己。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def sortedListToBST(self, head: ListNode) -> TreeNode:
        if head is None:
            return 
        if head.next is None:
            return TreeNode(head.val)
        slow = head
        fast = head.next.next
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        # tmp points to root
        tmp = slow.next
        # cut down the left child
        slow.next = None
        root = TreeNode(tmp.val)
        root.left = self.sortedListToBST(head)
        root.right = self.sortedListToBST(tmp.next)
        return root
  

保姆级教程:用BMS的HVIL检测电路,手把手教你排查高压互锁故障(附PWM/直流源方案对比) 本文详细解析了BMS中的HVIL检测电路,通过对比直流源与PWM两种方案,提供了一套系统化的高压互锁故障诊断流程。从电路原理到实战技巧,帮助维修人员精准定位故障根源,提升电动汽车高压系统的安全性和可靠性。 阅读详情

相关推荐

电机系列(1) - foc最基本原理、clark变换 、park变换、附代码

什么是FOC? FOC(Field-Oriented Control),直译是磁场定向控制,也被称作矢量控制 (VC,Vector Control) ,是目前无刷直流电机(BLDC)和永磁同步电机(PMSM)高效控制的最优方法之一。FOC旨在通过精确地控制磁场大小与方向,使得电机的运动转矩平稳、噪声小、效率高,并且具有高速的动态响应。 简单来说就是,FOC是一种对无刷电机的驱动控制方法,它可以让我们对无刷电机进行 “像素级” 控制,实现很多传统电机控制方法所无法达到的效果。 因为所谓的“矢量控制”其实就是

nepqiu的博客 7万+

LeetCode109: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 ...

卷卷萌的博客 208

《揭秘 BME688人工智能压力湿度温度气体传感器:气体检测的多面能手》:此文为AI自动生成

BME688 是博世推出的一款具有人工智能和集成高线性度高精度压力、湿度、温度传感器的气体传感器,专为移动和联网应用开发,尺寸紧凑且低功耗。BME688 作为一款先进的传感器,在多个领域展现出卓越的性能。它采用了坚固而紧凑的 3.0 x 3.0 x 0.9 mm³ 封装,这种小巧的尺寸使其能够轻松应用于对空间要求严格的移动设备和联网应用中。同时,低功耗的特性也使得它在能源有限的环境下能够长时间稳定运行。

zheng_ruiguo的专栏 1956

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

Leo_Chen008的博客 260

保姆级教程:用AdGuard DNS代理实现全设备广告过滤(含Win/Mac/安卓/iOS配置)

本文提供了一份详尽的AdGuard DNS代理配置指南,帮助用户在全设备(Win/Mac/安卓/iOS)上实现广告过滤。通过系统级DNS设置,AdGuard能有效拦截广告、跟踪器和恶意网站,提升隐私保护和浏览效率。教程涵盖原理说明、各平台配置步骤及高级优化技巧,适合不同技术水平的用户。

weixin_42656416的博客 888

抖音私信卡片私信名片的原理分析

简单说一下它的功能吧:抖音私信卡片,解决了客户封号严重、引流效率低的痛点,所以从去年到现在,依然是热销品。"抖音快手私信名片链接跳转"是2022年抖音快手引流最新技术,可以生成卡片链接,支持标题、描述、logo以及跳转落地页的完全自定义配置.支持微信公众号和微信号复制唤起微信客户端app添加,同时也支持任意链接落地页跳转提高粉丝引流转化率,关注转化率,引流转化率,安全绿色私信卡片功能1、跳转微信用户点击卡片,自动跳转微信界面,显示好友和群聊二维码2、跳转链接用户点击卡片,自动跳转到指定链接,支持任意链接。3

m0_71385839的博客 5195

leetcode[109]:Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search TreeGiven a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for singly-linked list. * struct

hahubaba的博客 345

mac(apple silicon)arm64安装spm12详细教程

3.下载matlab runtime R2023b for macOS (macA64),注意必须下载这个,只下载matlab是不可以的。默认安装目录是/Applications/MATLAB/MATLAB_Runtime/R2023b。注意最后的命令,路径根据自己的matlab_runtime安装位置进行调整,然后就可以安装成功了。5. 解压spm12.zip,解压后将文件夹拖到应用程序下(Applications)2.下载spm12,这个版本可兼容mac芯片!6.打开mac终端,输入。

weixin_62332001的博客 2036

LeetCode: Convert Sorted List to Binary Search Tree [109]

【题目】 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 【题意】 将一个有序链表转换成平衡二叉树 【思路】 思路跟Convert Sorted Array to Binary Search Tree完全一样

Harry Huang 919

从零搭建IoT数据中台:STM32+WebSocket实现边缘设备与Web端实时双向通信

本文详细介绍了如何从零搭建基于STM32和WebSocket协议的IoT数据中台,实现边缘设备与Web端的实时双向通信。通过硬件选型、协议设计、服务器搭建及安全优化,提供了一套低延迟、高并发的企业级解决方案,适用于智能农业和工业监控等场景。

tea88的博客 754

LVGL(lv_canvas)

这篇文章介绍了LVGL图形库中的lv_canvas组件,主要内容包括: lv_canvas是一个绘图缓冲区对象,提供丰富的绘图API,可以理解为LVGL的"离屏画布"。 基本使用流程:创建缓冲区->创建canvas对象->设置缓存和尺寸->进行绘图操作。 详细列举了常用的绘图函数,如绘制线条、矩形、文字、图片等。 提供了示例代码展示如何创建画布并绘制一条红色线段。 介绍了canvas的典型应用场景,如动态波形绘制、自定义控件底图等。 对比了canvas与普通img的区别

m0_49476241的博客 1042

leetcode 109: Convert Sorted List to Binary Search Tree

Use the fast slow method to find the mid point of the list and make it the root. Then do it recursively on both sides. /** * Definition for singly-linked list. * struct ListNode { * int val;

mikefan1991的博客 490

用 ABAP LSMW 的 Standard Batch / Direct Input 稳准快迁移物料主数据:从 MM01 录制到 RMDATIND 直入的落地指南

本文详细介绍了SAP数据迁移中LSMW工具的使用方法,重点分析了物料主数据迁移的两种主要路径:Batch Input Recording和Standard Batch/Direct Input。前者通过录制事务操作实现快速起步,后者通过调用预置接口程序实现稳定高效的批量处理。文章深入解析了Standard Direct Input的具体实施步骤,包括对象属性设置、源结构定义、字段映射规则等关键环节,并特别强调了迁移前的准备工作(如编号策略、视图策略等)对项目成功的重要性。通过对比两种方法的优缺点,为不同业务

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 162

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.          从给定的有序链表生成平衡二叉树。 分三步: 1. Get the number of nodes获取链表大小n 2. make a balance

学习之旅 376

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

python写算法题:leetcode: 109. Convert Sorted List to Binary Search Tree

class Solution(object): def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ if len(nums)==0: return None pos=len(nums...

224

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

[LeetCode] 109. Convert Sorted List to Binary Search Tree_Medium tag: Linked List

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 ...

weixin_30780221的博客 116

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

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. 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 ...

weixin_30326745的博客 104

leetcode: 109. Convert Sorted List to Binary Search Tree

Difficulty Medium. Problem 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 ...

JNing 251

Leetcode: Convert sorted list to binary search tree (No. 109)

Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9/22/2015 Go over one example to build some muscle memory about this bottom up, O...

weixin_30770495的博客 164

[LeetCode]题解(python):109-Convert Sorted List to Binary Search Tree

题目来源:   https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意分析:   给定一个排好序的链表,将这个链表转换成一个高度平衡树。 题目思路:   有一个偷懒的方法,将链表转换成一个数组,然后用上一题的解法解决。 代码(python):    # ...

weixin_34268610的博客 93

leetcode_c++:链表:Convert Sorted List to Binary Search Tree(109)

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. o(n) * 双指针 * 找到中数,然后递归

LandscapeMi 323

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. 分析如下: 第一个想法是可以借用上一题的结论,所以可以很快改出一个代码,提交了。但是上一题的输入是排序好的数组,上一题的时间复杂度是O(N),本题的输入是排序

BUILD 3540

LeetCode --- 109. Convert Sorted List to Binary Search Tree

题目链接: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. 这道题的要求是将有序链表转化成高度平衡的二叉搜索树(BST)。 1. 利

makuiyu的专栏 2241

【LeetCode】109. Convert Sorted List to Binary Search Tree

将list改为链表后,无法在时间内完成对元素的读写。 两种解法: 1. 暴力法 按照把列表转换为BST的方法,递归实现,每次遍历一般的列表长度找到中间元素,则时间复杂度为,n次遍历,遍历平均长度为,空间复杂度为递归栈所需的。 2. 自底向上递归法。不采用暴力法中自顶向下生成树的方法,而自底向上生成。每次在自顶向下的方法中受到阻碍时都应尝试一下自低向上方法。但总体来说,自顶向下方法应...

weixin_42344158的博客 391

LeetCode //C - 109. Convert Sorted List to Binary Search Tree

【代码】LeetCode //C - 109. Convert Sorted List to Binary Search Tree。

Made in Code 926

【Leetcode】109. Convert Sorted List to Binary Search Tree

题目地址: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 将有序链表转换成一个二叉搜索树。可以用分治法。先找到链表中点之前的那个node,然后以中点的值为root,对中点右半边建树,断开前半边和中点,接着对左半边建树,最后把两个树的树根分别作为root的左右孩子加到root上去,最后把root返回。...

数学、算法爱好者的博客 267
上一篇: LeetCode: 108. Convert Sorted Array to Binary Search Tree
下一篇: LeetCode: 111. Minimum Depth of Binary Tree
liveway6
博客等级 码龄11年 40粉丝 125原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值