当前位置:网站首页>leetcode:701. 二叉搜索树中的插入操作【bst的插入】
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
2022-07-03 00:46:00 【白速龙王的回眸】

分析
注意,bst的插入直接插叶子
无需像删除一样重构的
这个需要注意
ac code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
# 无需重构直接插入到叶子
if not root: return TreeNode(val)
def dfs(node):
if val <= node.val:
# 直接插入叶子
if not node.left:
node.left = TreeNode(val)
# 放到下一层再说
else:
dfs(node.left)
else:
if not node.right:
node.right = TreeNode(val)
else:
dfs(node.right)
dfs(root)
return root
总结
bst插入常识
边栏推荐
- Linear programming of mathematical modeling (including Matlab code)
- 研发一款国产ARM智能边缘计算网关需要什么
- Rust ownership (very important)
- 数学建模之线性规划(含MATLAB代码)
- Foundations of data science is free to download
- Usage of using clause in kingbases alter table
- 1.12 - Instructions
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- Explain the basic concepts and five attributes of RDD in detail
- 1.11 - 总线
猜你喜欢

Basic use of sringcloud & use of component Nacos

1.12 - 指令
![[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)](/img/f5/3ec22f1480227f33a1c8ac457155ed.jpg)
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)

【AutoSAR 十三 NVM】

Hdu3507 (slope DP entry)
![[AUTOSAR I overview]](/img/e4/b97c6beebf6f431d2d7cf209c6683e.png)
[AUTOSAR I overview]

【AutoSAR 七 工具链简介】

基于ARM RK3568的红外热成像体温检测系统

拥抱平台化交付的安全理念
![[AUTOSAR twelve mode management]](/img/42/292e3da3f5d488a1e8c10ea9bbfbab.png)
[AUTOSAR twelve mode management]
随机推荐
Basic use of sringcloud & use of component Nacos
合并K个已排序的链表
Vulkan is not a "panacea"“
Advanced pointer (I)
基于ARM RK3568的红外热成像体温检测系统
The difference between tail -f, tail -f and tail
Rk3568 development board evaluation (II): development environment construction
Leetcode-1964: find the longest effective obstacle race route to each position
Usage of using clause in kingbases alter table
链表内指定区间反转
研发一款国产ARM智能边缘计算网关需要什么
【AutoSAR 六 描述文件】
链表中的节点每k个一组翻转
Leetcode-2280: represents the minimum number of line segments of a line graph
RK3568开发板评测篇(二):开发环境搭建
【C语言】分支和循环语句(上)
cordova-plugin-device获取设备信息插件导致华为审核不通过
[daily training] 871 Minimum refueling times
Win10 多种方式解决无法安装.Net3.5的问题
Rust string slicing, structs, and enumeration classes