当前位置:网站首页>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插入常识
边栏推荐
- 【AutoSAR 一 概述】
- [AUTOSAR nine c/s principle Architecture]
- 指针进阶(一)
- Infrared thermography temperature detection system based on arm rk3568
- 465. DFS backtracking of optimal bill balance
- Teach you JDBC hand in hand -- structure separation
- Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
- 链表中的节点每k个一组翻转
- Vulkan performance and refinement
- 【AutoSAR 四 BSW概述】
猜你喜欢
[AUTOSAR I overview]
图解网络:什么是虚拟路由器冗余协议 VRRP?
Rk3568 development board evaluation (II): development environment construction
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
[AUTOSAR XIII NVM]
[overview of AUTOSAR four BSW]
Foundations of data science is free to download
测试右移:线上质量监控 ELK 实战
[shutter] image component (cached_network_image network image caching plug-in)
【AutoSAR 三 RTE概述】
随机推荐
[AUTOSAR nine c/s principle Architecture]
12_微信小程序之微信视频号滚动自动播放视频效果实现
全志A40i/T3如何通过SPI转CAN
Leetcode-849: maximum distance to the nearest person
1038 Recover the Smallest Number
链表中的节点每k个一组翻转
leetcode-849:到最近的人的最大距离
(C语言)数据的存储
[overview of AUTOSAR three RTE]
[love crash] neglected details of gibaro
Leetcode 294. Flip game II (game theory)
【AutoSAR 八 OS】
1.12 - Instructions
1038 Recover the Smallest Number
[jetcache] jetcache configuration description and annotation attribute description
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
Teach you JDBC hand in hand -- structure separation
数据分析思维分析犯法和业务知识——分析方法(一)
【C语言】分支和循环语句(上)