当前位置:网站首页>leetcode:701. Insertion in binary search tree [BST insertion]
leetcode:701. Insertion in binary search tree [BST insertion]
2022-07-03 01:06:00 【White speed Dragon King's review】
analysis
Be careful ,bst Insert directly into leaves
There is no need to refactor like delete
This needs attention
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:
# Insert directly into the leaf without refactoring
if not root: return TreeNode(val)
def dfs(node):
if val <= node.val:
# Insert leaves directly
if not node.left:
node.left = TreeNode(val)
# Put it on the next floor
else:
dfs(node.left)
else:
if not node.right:
node.right = TreeNode(val)
else:
dfs(node.right)
dfs(root)
return root
summary
bst Insert common sense
边栏推荐
- 1038 Recover the Smallest Number
- Test shift right: Elk practice of online quality monitoring
- 18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
- Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
- lex && yacc && bison && flex 配置的問題
- leetcode-224:基本计算器
- Rk3568 development board evaluation (II): development environment construction
- 全志A40i/T3如何通过SPI转CAN
- How to convert Quanzhi a40i/t3 to can through SPI
- RK3568开发板评测篇(二):开发环境搭建
猜你喜欢
随机推荐
异步、邮件、定时三大任务
Test shift right: Elk practice of online quality monitoring
合并K个已排序的链表
Leetcode-2280: represents the minimum number of line segments of a line graph
Key detection and sinusoidal signal output developed by Arduino
Teach you JDBC hand in hand -- structure separation
excel IF公式判断两列是否相同
数组与集合性能比较
tail -f 、tail -F、tailf的区别
【AutoSAR 九 C/S原理架构】
Win10 can't be installed in many ways Problems with NET3.5
excel去除小数点后面的数据,将数字取整
Inversion de l'intervalle spécifié dans la liste des liens
深度剖析数据在内存中的存储
1.12 - 指令
鏈錶內指定區間反轉
The difference between relational database and non relational database
【AutoSAR 六 描述文件】
Vulkan practice first bullet
【AutoSAR 一 概述】