当前位置:网站首页>【leetcode】701. Insert operation in binary search tree
【leetcode】701. Insert operation in binary search tree
2022-06-26 15:37:00 【liiiiiiiiiiiiike】
For details, please refer to 701. Insert operation in binary search tree
Their thinking
- BST characteristic , Judge root.val and val Size ,root.val < val, explain val Need to put root.right
- conversely Put it in root.left, Judge root.right Whether there is , If there is a direct release , Otherwise, recursion insert function , Find the right place
# 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 insert(root):
if root.val < val:
if root.right:
insert(root.right)
else:
root.right = TreeNode(val)
else:
if root.left:
insert(root.left)
else:
root.left = TreeNode(val)
insert(root)
return root
边栏推荐
- [wechat applet] event binding, do you understand?
- Seurat to h5ad summary
- On which platform is it safe to buy shares and open an account? Ask for guidance
- 1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)
- Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved
- 音视频学习(二)——帧率、码流和分辨率
- Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)
- Applicable and inapplicable scenarios of mongodb series
- How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
- 面试高频 | 你追我赶的Flink双流join
猜你喜欢
Restcloud ETL resolves shell script parameterization
Don't remove custom line breaks on reformat
【TcaplusDB知识库】TcaplusDB运维单据介绍
A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
Inaccurate data accuracy in ETL process
【ceph】CephFS 内部实现(二):示例--未消化
Audio and video learning (I) -- PTZ control principle
[CEPH] Introduction to cephfs caps
Evaluate:huggingface评价指标模块入门详细介绍
Utilisation d'abortcontroller
随机推荐
有Cmake的工程交叉编译到链接时报错找不到.so动态库文件
Don't remove custom line breaks on reformat
Unity C # e-learning (10) -- unitywebrequest (1)
PHP file upload 00 truncation
【SNMP】snmp trap 介绍、安装、命令|Trap的发送与接收代码实现
JS之事件
High frequency interview 𞓜 Flink Shuangliu join
One click GCC script installation
golang 临时对象池优化
Using restcloud ETL shell component to schedule dataX offline tasks
音视频学习(一)——PTZ控制原理
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure
Mongodb series window environment deployment configuration
Cache page keepalive use in Vue
【TcaplusDB知识库】TcaplusDB系统管理介绍
【TcaplusDB知识库】TcaplusDB运维单据介绍
JS simple deepcopy (Introduction recursion)
AbortController的使用
【小程序实战系列】小程序框架 页面注册 生命周期 介绍
Inaccurate data accuracy in ETL process