当前位置:网站首页>【近日力扣】验证二叉搜索树+将有序数组转换为二叉搜索树
【近日力扣】验证二叉搜索树+将有序数组转换为二叉搜索树
2022-04-22 04:05:00 【foolBirdd】
- 总结:遇到树的题,可以先思考用哪种遍历方法,感觉遍历是常规解法
验证二叉搜索树(树,简单)
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
假设一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
- 思路:很容易想到利用遍历,判断左子树值是否小于当前节点的,右子树值是否大于当前节点的,是的话继续分别遍历左右节点,直到不符合上述判断条件或者当前节点为 null(意味着遍历完成)
var isValidBST = function (root) {
// 此处 low 和 up 的含义想通了就思路清晰了
var erdogic = (root, low, up) => {
if (root === null) return true
if (root.val < low || root.val > up) return false
// 对于左节点,第二个参数一直是 low,意味着递归只用来判断左子树值是否小于它的父节点值
// 对于右节点,第三个参数一直是up,~判断右子树值是否大于它的父节点值
return erdogic(root.left, low, root.val) || erdogic(root.right, root.val, up)
}
return erdogic(root, -Infinity, Infinity)
}
将有序数组转换为二叉搜索树(数组,简单)
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
- 思路:首先要定好根节点,由于是二叉搜索树,所以最好定升序数组的中位元素为根节点,则其左边元素设为左子树,右边元素同理,如此则遍历出一颗树。
var sortedArrayToBST = function (nums) {
let erdogic = (left, right) => {
if (left > right) return null
let mid= Math.floor((left + right) / 2)
let root = new TreeNode(nums[mid])
root.left = erdogic(left, mid - 1)
root.rigth = erdogic(mid + 1, right)
return root
}
return erdogic(0, nums.length - 1)
}
版权声明
本文为[foolBirdd]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43510829/article/details/118194976
边栏推荐
- Registration process of New Zealand company and materials required
- Ivorysql 1.2 has come
- Convenience stores are crazy: convenience bee, Rosen and Yijie "fierce battle"
- DR/AP4029 outdoor IPQ-4019/4029 Outdoor directional-antennas
- Optimisation des performances des pages Web
- Do447ansible tower navigation
- 射频微波设计软件
- Zhongshanghui ⺠ evolution of trading platform architecture: response to Apache shardingsphere
- Implement joint type verification of parameters in nest
- Lammps实现结冰匀相成核
猜你喜欢

机器学习系列(5)_特征工程03碳排放小案例

2022-04-21: given a blacklist containing non repeating integers in [0, n), write a function to return a random integer not in the blacklist from [0, n), and optimize it to minimize the number of calls

Bubble ranking and running for president

Autodesk genuine service2020 delete

How do programmers ensure that software is free of bugs?

Class组件详解

On the origin of wireless operation and maintenance and project construction

JS dynamically generates a table and adds a scroll bar

Spark quick start series (5) | spark environment construction - standalone (2) configuring history log server

Mongodb - $match operation of aggregation pipeline
随机推荐
JS dynamically generates a table and adds a scroll bar
Common tool NC Wireshark rebound shell
The United States raised interest rates and devalued the RMB, but such products ushered in a honeymoon period
Header song answer (string basic operation)
Zhongtian steel holds "golden cup" in 18 products
sumo-绕圈行驶
js动态生成table表格,加滚动条
便利店卷疯了:便利蜂、罗森、易捷“激战”
Insert a number into the ordered array (Bubble + rand function)
Machine learning theory (6): from logistic regression (logarithmic probability) method to SVM; Why is SVM the maximum interval classifier
Determination of bipartite graph by coloring method
. net debugging: use visual studio to debug dump files
机器学习系列(5)_特征工程03碳排放小案例
How to generate PCB real-time snapshot in 3D in Ad
win11系统开机后没有输入法——解决方法亲测有效
The WiFi button of win11 is missing and cannot be connected to the Internet
Stc8a8k64d4 (51 Series MCU) printf printing data abnormal problem
Go gin framework configuration log output to file
Daily practice (46): intersection of two arrays
[raspberry pie C language development] experiment 12: pcf8591 analog-to-digital converter module