首页 > 其他分享 >【刷题笔记】108. Convert Sorted Array to Binary Search Tree

【刷题笔记】108. Convert Sorted Array to Binary Search Tree

时间:2023-11-13 12:02:43浏览次数:37  
标签:Binary Convert TreeNode nums Tree len height balanced 10

题目

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

题目大意

将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

解题思路

  • 把一个有序数组转换成高度平衡的二叉搜索数,按照定义即可

参考代码

package leetcode

import (
	"github.com/halfrost/LeetCode-Go/structures"
)

// TreeNode define
type TreeNode = structures.TreeNode

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

func sortedArrayToBST(nums []int) *TreeNode {
	if len(nums) == 0 {
		return nil
	}
	return &TreeNode{Val: nums[len(nums)/2], Left: sortedArrayToBST(nums[:len(nums)/2]), Right: sortedArrayToBST(nums[len(nums)/2+1:])}
}

标签:Binary,Convert,TreeNode,nums,Tree,len,height,balanced,10
From: https://blog.51cto.com/u_16110811/8340480

相关文章

  • TreeSet
    TreeSet的基本操作放到TreeSet集合中的元素:无序不可重复,但是可以按照元素的大小顺序自动排序(默认升序)//集合的创建TreeSet<Integer>treeSet=newTreeSet<>();//添加元素treeSet.add(1);treeSet.add(10);treeSet.add(199);treeSet.add(41);treeSet.add(0);//遍......
  • 数据存储和检索:B-tree 和 LSM-tree
     本文主要介绍数据库的核心数据结构索引的实现方式:B+tree和LSM-tree。实际上,数据库是可以不存在索引结构的,遍历数据库总归可以实现数据库的查询,但是,如果数据量很大,这种低效的做法是不可接受的,那么自然而然,牺牲部分空间换取时间被提出和接受,即保留额外的元数据,实现数据......
  • 导出相关binary制作k8s本地包
    这次把我之前安装的k8s的包导出来保存。cd/var/cache/apt/lskube*cd~&&mkdirk8sdbinstallsudocp*_1.23.17*~/k8sdbinstall 接下来保存相关的docker镜像, 使用dockersave-oexport.tarimageId/imageName:tagName保存多个镜像,导出全部镜像,需要切换到su执行#......
  • CF1304E 1-Trees and Queries(lca+树上前缀和+奇偶性)
    题目二话不说,直接按题意模拟暴搜,当然\(O(nq)\)的复杂度显然是寄了的。不过,在模拟的过程中,我在链式前向星的删边中居然一开始错了,还是要mark一下以后注意。voiddel(intx,intpre){ e[top].to=e[top].next=0; h[x]=pre;//h[x]=0;<---tip}intmain(){ ......
  • Planting Trees and Glasses--- Holding Back Soil Erosion
    Plantingtreesandglasses植树种草 Ganzhou, a typical red soil hilly area in Jiangxi province, is a pilot area for high-quality development of soil and water conservation in China. Through a series of following innovative in......
  • 【刷题笔记】104. Maximum Depth of Binary Tree
    题目Givenabinarytree,finditsmaximumdepth.Themaximumdepthisthenumberofnodesalongthelongestpathfromtherootnodedowntothefarthestleafnode.Note:Aleafisanodewithnochildren.Example:Givenbinarytree[3,9,20,null,null,15,7],......
  • 树状数组(Binary Index Tree)
    一、问题引入LoguP3374模版题--树状数组。初始化一个数组,接下来进行若干次以下操作:单点修改:将某个元素的值进行修改区间访问:返回该区间的总和问题分析如果通过简单索引操作,“1”的时间复杂度为O(1),“2”的时间复杂度为O(n),其中如果使用一个dp表的方式来存储前n项之和,那么“......
  • linux ImageMagick convert 报错 convert-im6.q16***
    在linux批量处理图片时候报一下错误,导致图片无法按要求转化,运行的命令如下:convert**.jpg-resize512x512new.jpg报错:convert-im6.q16:cacheresourcesexhausted`*.jpg'@error/cache.c/OpenPixelCache/4083.convert-im6.q16:noimagesdefined`./zoom/113.jpg'@erro......
  • [题解]CFgym103470E Paimon Segment Tree
    PaimonSegmentTree区间加,求一段时间内的区间平方和。\(n,m,q\le5\times10^4\)。对时间维差分一下,变成询问区间历史平方和。离线下来扫描线,扫描线维护时间维,数据结构维护序列维。考虑维护二元组\((a,s)\)表示当前位置值为\(a\),历史平方和为\(s\)。可以发现怎......
  • vue 项目使用element ui 中tree组件 check-strictly 用法
    属性check-strictly:   在显示复选框的情况下,是否严格遵循父子互相关联的做法,默c认为false。   默认false,父子关联。      点击父节点,其下子节点全部统一跟随父节点变化,点击子节点,子节点部分勾选时,父节点处于半选状态。   设置为true,严格遵循......