LeetCode: 108. Convert Sorted Array to Binary Search Tree

Open3D——RANSAC 三维点云空间直线拟合【2025最新版】 使用RANSAC算法在点云数据中拟合三维空间直线的python代码实现。博客长期更新,本文最新更新时间为:2025年3月23日。 阅读详情

题目

Given an array 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 array: [-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

解题思路

取数组的中位数处理,分左右两个节点依次递归就好

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

class Solution:
    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
OpenHarmony 显示Display驱动全栈解析:DisplayLayer + Gralloc + Gfx 三位一体,打造高性能图形底座 OpenHarmony Display显示驱动模块是系统图形渲染和显示功能的基础组件,负责管理显示设备、图层渲染、内存分配等核心功能。该模块采用HDI(Hardware Driver Interface)设计架构,为上层图形服务提供统一的显示能力接口,实现了显示设备的初始化、图层管理、内存分配与映射、硬件加速等关键功能。DisplayLayer:负责显示图层的管理, 包括图层创建、显示属性设置、图层填充及刷新等操作; 阅读详情

相关推荐

matlab特征提取和分类_SAST Weekly|MATLAB机器学习初探——做个心音分类器

SAST weekly 是由电子工程系学生科协推出的科技系列推送,内容涵盖信息领域技术科普、研究前沿热点介绍、科技新闻跟进探索等多个方面,帮助同学们增长姿势,开拓眼界,每周更新,欢迎关注!欢迎愿意分享知识的同学投稿至 eesast@mail.tsinghua.edu.cn , 期待你的作品!本期的SAST weekly,想跟大家分享一下如何用MATLAB进行初步的机器学习,构建一个正常/...

weixin_36453829的博客 5587

LeetCode: Convert Sorted Array to Binary Search Tree [108]

【题目】 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 【题意】 给定一个已排序的数组(不存在重复元素),将它转换成一棵平衡二叉搜索树。 【思路】 由于平衡二叉树要求左右子树的高度差绝对值相遇等于1,也就是说左右子树尽可能包含相同数目节点。 则使用二分法来解本题即可。

Harry Huang 950

告别底噪和发热:TPA3255的PCB布局与散热设计保姆级教程(附嘉立创EDA文件)

本文详细解析TPA3255 Class D音频功放的PCB布局与散热设计,提供从电源系统分层架构到热管理系统的全流程优化方案。通过实战案例展示如何消除底噪、降低发热,并附嘉立创EDA文件,助力工程师快速设计高性能音频功放。

weixin_29208077的博客 389

LeetCode 108:Convert Sorted Array to Binary Search Tree

The Problem can be seen in Convert Sorted Array to Binary Search Tree,the analysis is shown in the following. A given array named as nums (like [-10,-3,0,5,9]) is sorted in ascending order. constructi...

forever_zzx的专栏 189

双内核架构 Xenomai 4 安装教程

双内核 Xenomai 4 安装指南

^_^ 4489

通信原理与MATLAB(八):2PSK的调制解调

通信原理和MATLAB

菜yuan~的博客 3万+

leetcode:108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree 玩完了,不会,,, 大佬的答案, class Solution { TreeNode* sortedArrayToBST(vector<int>& nums, int start, int end){ if(end<=start) r...

hunter 166

闲置服务器之代理服务器篇

这台服务器是在阿里云免费白嫖的,天工开物计划,阿里给的300块无门槛优惠券,买了一台新加坡的境外服务器,288,刚刚抵扣完,因为是境外服务器,能够访问一些国内不能访问的网站,所以本文章仅从技术的角度来搭建代理服务器,使用时,请自觉遵守国家相关规定,如有任何违法行为,与本人无任何关系!!!。本文使用tinyproxy来搭建代理服务器。

m0_50281408的博客 2119

LeetCode之路:108. Convert Sorted Array to Binary Search Tree

LeetCode之路:108. Convert Sorted Array to Binary Search Tree 一、引言真是很久很久没有刷 LeetCode 了呢!自从掉入了 Cef3 的坑之后,又加上期间看完了 《COM技术内幕》之后,又看了点《C++语言的设计和演化》,仿佛都将刷 LeetCode 这件事甩到脑后去了。虽然看书能够很好的提升自己,但是刷题也不失为一种雅兴。不得不说,这道题还是挺有趣的

(ÒωÓױ) 2174

YOLOv11改进策略【卷积层】| CVPR-2024 PKI Module 获取多尺度纹理特征,适应尺度变化大的目标

PKI Module是。

Limiiiing的博客 2727

【Oracle】Oracle rman工具使用(真的细)

Oracle RMAN(Recovery Manager)是Oracle数据库备份和恢复的官方工具,它提供的备份和恢复功能非常强大和可靠。RMAN可以备份整个数据库或特定表空间、数据文件、控制文件和归档日志,同时还可以进行增量备份和增量恢复。RMAN还提供了诸如备份验证、备份优化、备份复制、备份集管理、恢复操作、自动备份、自动恢复等高级功能。它使用的备份和恢复格式是Oracle通用的备份集(Backup Set),这使得备份和恢复非常灵活和高效。

tttzzzqqq2018的博客 8519

Leetcode-108: Convert Sorted Array to Binary Search Tree

解法1:我用的递归。#include &lt;iostream&gt;#include &lt;vector&gt;using namespace std;struct TreeNode {    int val;    TreeNode *left;    TreeNode *right;    TreeNode(int x) : val(x), left(NULL), right(NULL) ...

weixin_38285631的博客 99

机器人控制算法九之 位姿描述与空间变换

机器人控制算法九之 位姿描述与空间变换

yohnyang的博客 1041

leetcode[108]:Convert Sorted Array to Binary Search Tree

Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for a binary tree node. * struct TreeNode {

hahubaba的博客 450

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

IEM模型,地表粗糙度模拟

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

LeetCode 108: Convert Sorted Array to Binary Search Tree

Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路 为了得到高度平衡的二叉树,选中点 middle=(start+end)/2middle = (start + e

一只码畜 283

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Leetcode 108: Convert Sorted Array to Binary Search Tree

问题描述: Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by mor

weixin_51401145的博客 148

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[leetcode: Python] 108. Convert Sorted Array to Binary Search Tree

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.方法一:性能92ms# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x

cyy2learn的博客 1034

LeetCode108:Convert Sorted Array to Binary Search Tree

Given an array 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...

卷卷萌的博客 187

leetcode 108: Convert Sorted Array to Binary Search Tree

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ clas

mikefan1991的博客 202

leetcode(108):Convert Sorted Array to Binary Search Tree

题目: Given an array 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 t

赵小越的博客 195

[leetcode]: 108. Convert Sorted Array to Binary Search Tree

1.题目Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 给一个有序数组,将其转换为一个高度平衡二叉搜索树2.分析BST的特点是左节点<根结点<右节点 左右子树平衡的话,尽可能两边的元素个数相近。所以每次选择数组中间的数作为根结点,左边元素分配到左子树

沐沐余风的专栏 419

leetcode: 108. Convert Sorted Array to Binary Search Tree

Difficulty Easy. Problem Given an array 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 ...

JNing 251

[LeetCode]: 108: Convert Sorted Array to Binary Search Tree

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 分析: 平衡二叉树的特点:左右子树的高度差不能超过1 采用二分查找思想和递归思想 代码: public static TreeNode sortedArrayT...

weixin_34061555的博客 77

LeetCode 108: Convert Sorted Array to Binary Search Tree DFS求解

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路: 1. 找到数组的中间节点将其置为根节点 2. 左边的即为左子树 3. 右边的即为右子树 4. 递归求解 /** * Definition for a b...

weixin_30426957的博客 64
上一篇: LeetCode: 107. Binary Tree Level Order Traversal II
下一篇: LeetCode: 109. Convert Sorted List to Binary Search Tree
liveway6
博客等级 码龄11年 40粉丝 125原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值