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 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* sortedListToBST(ListNode* head) {
		if (!head) return NULL;
		vector<int> tmp; tmp.push_back(0);
		while (head) {
			tmp.push_back(head->val);
			head = head->next;
		}	
		function<TreeNode*(int, int)> dfs = [&dfs, &tmp](int lb, int ub) -> TreeNode* {
			if (lb > ub) return NULL;
			int m = (lb + ub) >> 1;
			auto p = new TreeNode(tmp[m]);
			p->left = dfs(lb, m - 1);
			p->right = dfs(m + 1, ub);
			return p;
		};
		return dfs(1, tmp.size() - 1);
	}
};

转载于:https://www.cnblogs.com/GadyPu/p/5612453.html

ollama如何在自己的服务器上跑Qwen3(小白必看) 在国内下载ollama最新安装包时,使用curl -fsSL https://ollama.com/install.sh | sh命令可能会遇到网络问题导致下载进度停滞。为了解决这一问题,可以通过国内的镜像源进行下载,例如使用modelscope社区提供的最新ollama镜像仓库。具体步骤包括:首先通过pip install modelscope安装modelscope,然后使用modelscope download命令下载ollama-linux镜像,接着进入下载目录并赋予安装脚本执行权限,最后运行安装脚 阅读详情

相关推荐

基于LabVIEW控制仪器检测与监控 --普源示波器DS1102Z-E

LabVIEW 控制普源示波器 代码实现与解释

s0101100的博客 4229

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

在win10-wsl2(ubuntu)中安装jlink

在 官方下载linux版本的deb包拷贝到ubuntu的/home目录下(其他目录也可以),进行安装然后提示很多依赖缺失的错误,此时执行自动安装需要的依赖包再次重新安装就可以啦查看是否安装成功,输入JLink 再按下两次Table键,是否如下所示这样表示安装成功了。

yaq_30401的博客 3135

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

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

motongxue的博客 260

遥感生态指数(RSEI)——四个指数的计算

遥感生态指数是在4个生态指数的基础上通过主成分变换得出的一个评价生态环境质量的指标。因此,第二部分就是要分别计算出这4个指数的值。 ①首先,是绿度指数的计算,一般用归一化植被指数(NDVI) NDVI=(NIR-Red)/(NIR+Red) 在Bandmath工具中输入NDVI计算公式 (float(b4-b3))/(b4+b3) 其中,b3、b4分别是红波段和近红外波段 ②其次,是湿度计算。 WET = c1B1 + c2B2 + c3B3...

qq_41978498的博客 9万+

Flannel,Calico 和 Cilium 之间有哪些优缺点和差别?

例如,在一个多租户的容器集群中,可以通过Calico为不同租户的容器设置精细的访问控制策略,确保每个租户的资源安全。例如,在设置复杂的网络策略和BGP路由时,需要对网络概念和Calico的配置语法有比较深入的了解。它可以根据容器的标签、服务等信息来定义访问规则,并且可以实现更加细粒度的网络安全策略,如HTTP层的策略。性能损耗:在overlay网络模式下(如VXLAN),由于需要对数据包进行额外的封装和解封装操作,会带来一定的性能损耗,尤其是在高流量的场景下,这种性能损耗可能会更加明显。

chinansa的博客 1529

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

华为数通方向HCIP-DataCom H12-831题库(多选题:121-140)

华为数通HCIP H12-831多选题

didiplus 1290

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

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

weixin_42344158的博客 391

AI智能体在化学中的伦理问题:7个案例,架构师如何应对?

AI在化学中的应用早已不是新鲜事:药物发现中用AI预测分子活性(比如AlphaFold预测蛋白质结构)、化工生产中用AI优化合成路线(降低成本30%+)、材料研发中用AI设计新型电池材料(缩短研发周期5年)……但随着AI从“辅助工具”升级为“自主决策智能体”(比如能自动调整反应条件的“化学机器人”),伦理风险开始从“潜在隐患”变成“真实事故”。AI在化学中会踩哪些伦理“坑”?(用案例具象化)这些“坑”的底层逻辑是什么?(从技术到价值观的链条)架构师能做什么?

AI 算法实战派 1361

【动态导通电阻】GaN HEMT动态导通电阻的精确测量

2023年7月,瑞士洛桑联邦理工学院的Hongkeng Zhu和Elison Matioli在《IEEE Transactions on Power Electronics》期刊发表文章,提出了一种稳态测量方法,用于研究氮化镓(GaN)高电子迁移率晶体管(HEMT)的动态导通电阻(Ron)。实验结果显示,商业GaN器件在稳态下的动态Ron变化显著,稳定时间可达数分钟且与电压相关。传统的脉冲测量方法由于测试时间不足,可能导致结果不准确。该

荷塘阅色 1376

【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

【AI应用实战-claude】在飞书上接入claudecode进行远程使用(十三)

本文介绍了三种将ClaudeCode接入飞书的方案,包括官方飞书CLI工具(推荐)、cc-connect中转工具和MCP协议集成。官方飞书CLI适合自动化办公场景,通过简单配置即可实现消息发送和文档管理;cc-connect适用于远程调用Claude功能,通过飞书群聊触发响应;MCP协议则适合开发者构建复杂工作流。文章提供了详细的操作步骤,并针对不同使用场景给出了选择建议。

1008

LeetCode 109. Convert Sorted List to Binary Search Tree

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

yc_cy1999的博客 322

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

[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

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

[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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

Leetcode 109. Convert Sorted List to Binary Search Tree

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

每天进步一点点 498

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

无名山丘,崛起成峰 1025

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

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

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

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

Made in Code 924

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

zxc120389574的博客 123

[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 108 Convert Sorted Array to Binary Search Tree
下一篇: leetcode 226 Invert Binary Tree
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值