当前位置:网站首页>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插入常识
边栏推荐
- 这不平凡的两年,感谢我们一直在一起!
- Leetcode-871: minimum refueling times
- Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- Leetcode-224: basic calculator
- Nacos+openfeign error reporting solution
- 1038 Recover the Smallest Number
- Meaning of Tencent cloud free SSL certificate extension file
- 瑞萨电子RZ/G2L开发板上手评测
- matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
猜你喜欢

Basic use of sringcloud & use of component Nacos

利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度

2022中国3D视觉企业(引导定位、分拣场景)厂商名单

The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022

瑞萨电子RZ/G2L开发板上手评测

1.11 - bus

【AutoSAR 十一 通信相关机制】

图解网络:什么是虚拟路由器冗余协议 VRRP?

研发一款国产ARM智能边缘计算网关需要什么

【AutoSAR 九 C/S原理架构】
随机推荐
[AUTOSAR VI description document]
线程的启动与优先级
基于ARM RK3568的红外热成像体温检测系统
leetcode-934:最短的桥
【AutoSAR 八 OS】
leetcode-2115:从给定原材料中找到所有可以做出的菜
Lex & yacc & bison & flex configuration problems
【AutoSAR 五 方法论】
[AUTOSAR XIII NVM]
删除有序链表中重复的元素-II
[AUTOSAR I overview]
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
Vulkan practice first bullet
Advanced pointer (I)
cordova-plugin-device获取设备信息插件导致华为审核不通过
Sentry developer contribution Guide - configure pycharm
Meaning of Tencent cloud free SSL certificate extension file
【AutoSAR 十 IO架构】
[C language] branch and loop statements (Part 1)