LeetCode109. Convert Sorted List to Binary Search Tree

【论文汇总】Diffusion Models视频生成/视频编辑/可控视频生成/跨模态视频生成 本文总结了Diffusion Models视频生成领域相关的工作,目前共收录142篇,持续更新中。 阅读详情

题目:

https://leetcode.com/problems/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.

思路:

题意:给一个排好序的链表,把它转化成高度平衡的二叉查找树。
其实就是不断的选出中间的节点作为根结点,分成两段,再分别往下递归选出中间节点。

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* partition(ListNode* head,int len){
        if(head==NULL || len<=0){
            return NULL;
        }
        ListNode* left=head;
        ListNode* right=head;
        int partLen=len/2;
        for(int i=1;i<=partLen;i++){
            right=right->next;
        }
        TreeNode* treeNode=new TreeNode(right->val);
        right=right->next;
        TreeNode* leftChild=partition(left,partLen);
        TreeNode* rightChild=partition(right,len-partLen-1);
        treeNode->left=leftChild;
        treeNode->right=rightChild;
        return treeNode;
    }
    TreeNode* sortedListToBST(ListNode* head) {
        int len=0;
        if(head==NULL){
            return NULL;
        }
        ListNode* p=head;
        while(p!=NULL){
            len++;
            p=p->next;
        }
        return partition(head,len);
    }
};
必知必会:DeepSeek-R1训练流程详解 阅读详情

相关推荐

【前端】【面试】【业务场景】前端如何获取并生成设备唯一标识

问题:前端如何获取并生成设备唯一标识?核心要点:浏览器原生信息有限,但通过组合多个维度可生成设备指纹(Device Fingerprint),用于唯一标识设备。常见方式浏览器信息(User-Agent)屏幕与系统参数本地存储标识(localStorage / cookie)第三方指纹库(如 FingerprintJS)方式优点缺点手动组合指纹实现简单不稳定,识别率低持久化强容易被用户清除识别率高、稳定性好依赖第三方库IP 地址识别网络识别粗略共享网络环境下重复可能大。

qq_59344127的博客 1793

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 3538

保姆级教程:用STM32CubeMX给STM32F407配置RTC秒中断,并解决掉电时间丢失问题

本文详细介绍了如何使用STM32CubeMX为STM32F407配置RTC秒中断,并解决掉电时间丢失问题。通过硬件原理与软件配置的双视角解析,帮助开发者实现精确计时与时间持久化,适用于物联网设备和工业控制器等场景。

weixin_42519733的博客 136

LeetCode 力扣 109. 有序链表转换二叉搜索树 convert sorted list to binary search tree

大家觉得写还可以,可以点赞、收藏、关注一下吧! 也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn 文章目录109. 有序链表转换二叉搜索树题目描述示例:1,快慢指针2. 数组方式提交结果链表快慢指针 109. 有序链表转换二叉搜索树 题目描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。 示例: 给定的有序链表: [-10,

motongxue的博客 260

头歌 DNS协议分析第3关----DNS 报文分析

DNS 报文分析

qq_67667368的博客 4346

Hyper-V创建Ubuntu虚拟机设置固定IP

本文记录使用hyper-v创建ubuntu虚拟机,为虚拟设置固定IP且通过物理机可访问。

2622

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

LeeWei 218

基于线性光耦HCNR200的高压直流采样电路设计与精度优化

本文详细介绍了基于线性光耦HCNR200的高压直流采样电路设计与精度优化方法。针对直流高电压测量中的安全与干扰问题,提出采用HCNR200光耦隔离方案,实现高精度线性传输。文章深入解析电路设计关键细节,包括电阻网络、运放选型、滤波电容配置等,并提供精度优化实战技巧与常见问题排查指南,助力新能源电动汽车和高压储能系统的高压直流采样应用。

weixin_42713608的博客 578

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

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

weixin_42344158的博客 391

3️⃣ Coze工作流基础教学(2025年全新版本)

Coze 工作流作为扣子平台的重磅工具,利用可视化节点,如大语言模型、插件、条件判断等,把复杂任务巧妙拆解成多步骤流程。它不仅支持自动化编排,还能实现跨系统协同,有效提升处理效率与输出稳定性。在那些对准确性或格式有严格要求的场景中,比如报告生成、旅行规划,Coze 工作流更是发挥着关键作用。

李同学Lino的博客 5850

Livox激光雷达传感器在Python3中的完整驱动程序:深入剖析OpenPyLivox库的实现和应用

当我们提到激光雷达或激光扫描技术时,Livox激光雷达传感器无疑是业界的佼佼者。尽管Livox提供了基于C++的官方软件和API,但现在,有了OpenPyLivox (OPL) 库,Python开发者们也能够轻松地接入和控制这些高级的激光雷达设备。

qq_38334677的博客 1002

【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

【android bluetooth 案例分析 01】【HiCar 详解 3】【hicar时如何交换Wifi参数的】

如果直接使用 L2CAP(不经过 RFCOMM),会需要额外的协议来保证可靠性,而 RFCOMM 天然支持。BLE 广播的数据长度受限(31 字节 / 255 字节扩展),不适合传输复杂的 Wi-Fi 连接参数。使用 RFCOMM 传输参数,可以兼容不同厂商的车机和手机,而无需依赖特殊的蓝牙功能。手机解析这些参数,并使用 Wi-Fi Direct 连接到车机。,然后才能让 Wi-Fi Direct 进行真正的数据传输。,适合传输 Wi-Fi 连接参数等关键数据。,可以安全地交换 Wi-Fi 连接信息。

奔跑吧 android 的博客 1740

LeetCode 109. Convert Sorted List to Binary Search Tree

文章目录题目描述知识点结果实现码前思考代码实现码后反思 题目描述 知识点 二分查找(Binary Search) 和 二分查找树(Binary Search Tree) 的关系 结果 时间空间都在范围之内,菜鸟我已经很满意了。。。 实现 码前思考 一开始以为这是一个典型的不能再典型的 平衡二叉树(AVL Tree) 的问题,后来思考了一下,这个题咋正确率有70%???AVL Tree应该...

yc_cy1999的博客 322

精雕软件(JDpaint) 5.5 绿色版

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

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

[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. * */ public class Conve

瓦尔登湖 772

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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

fanyuwgy的博客 160

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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

原题链接: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 1. 题目介绍 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For...

Ber03的博客 228

Leetcode 109. Convert Sorted List to Binary Search Tree

题目:将一个升序的单向链表,转换成平衡的二叉搜索树。 旧思路:就像108题一样,我们一般做的是把数组转成二叉树,但是一个单向链表,我刚开始的思路是也像数组一样,就是每次找a[i]的时候,都要从表头开始找到第i个节点,但是这种思路显然很笨。 新思路:这个是在网上看的别人的代码,虽然关键的代码只有七行,但是已经被原作者那强大的思维能力深深震撼了,虽然也是用的递归,但是递归我从来没这么用过。

每天进步一点点 498

109.Convert Sorted List to Binary Search Tree Leetcode Python

Convert Sorted List to Binary Search Tree Total Accepted: 32343 Total Submissions: 117376 My Submissions Question Solution  Given a singly linked list where elements are sorted in ascending order, co

hyperbolechi的专栏 1191

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的专栏 2238

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. 有序链表转二叉搜索树 利用108题代码,将链表转成数组。至于有用快慢指针做的,我个人认为,完全是吃饱了撑的。 /** * Definition for singly-l

无名山丘,崛起成峰 1026

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in asc...

dong12276的博客 139

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

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

Made in Code 924

LeetCode109—Convert Sorted List to Binary Search Tree

leetcode109 convert sorted list to binary search tree

上善若水 484

[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. /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis

xinxinxv的专栏 383

leetcode No109. Convert Sorted List to Binary Search Tree

Question: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 把有序链表转化成平衡的BST Algorithm: 把链表转化成数组,再根据leetcode No108. Convert Sort

Dufre 1099
上一篇: LeetCode210. Course Schedule II
下一篇: LeetCode130. Surrounded Regions
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值