当前位置:网站首页>【leetcode】701. 二叉搜索树中的插入操作
【leetcode】701. 二叉搜索树中的插入操作
2022-06-26 15:21:00 【liiiiiiiiiiiiike】
题目详见 701. 二叉搜索树中的插入操作
解题思路
- BST特性,判断root.val和val的大小,root.val < val, 说明val需要放到 root.right
- 反之 放到root.left,判断root.right是否存在,如果存在直接放,否则就递归insert函数,找到合适位置
# 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
边栏推荐
- Mongodb series window environment deployment configuration
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
- 【ceph】CEPHFS 内部实现(一):概念篇--未消化
- vsomeip3 双机通信文件配置
- 【SNMP】snmp trap 介绍、安装、命令|Trap的发送与接收代码实现
- 【TcaplusDB知识库】TcaplusDB系统管理介绍
- Seurat to h5ad summary
- Unity C # e-learning (VIII) -- www
- Evaluate:huggingface评价指标模块入门详细介绍
- Comparative analysis of restcloud ETL and kettle
猜你喜欢

Using restcloud ETL shell component to schedule dataX offline tasks

数据库-视图

【小程序实战系列】小程序框架 页面注册 生命周期 介绍

HR export data Excel VBA
![[applet practice series] Introduction to the registration life cycle of the applet framework page](/img/82/5a9219a7c2ee211180cc4b2d138204.png)
[applet practice series] Introduction to the registration life cycle of the applet framework page

Ansible自动化的运用

音视频学习(三)——sip协议

A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
![[C language practice - printing hollow upper triangle and its deformation]](/img/56/6a88b3d8de32a3215399f915bba33e.png)
[C language practice - printing hollow upper triangle and its deformation]

效率超级加倍!pycharm十个小技巧就是这么神
随机推荐
RestCloud ETL解决shell脚本参数化
【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍
【ceph】CephFS 内部实现(三):快照
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
Unity unitywebrequest download package
Evaluation - TOPSIS
效率超级加倍!pycharm十个小技巧就是这么神
# 粒子滤波 PF——三维匀速运动CV目标跟踪(粒子滤波VS扩展卡尔曼滤波)
Database - sequence
JS simple deepcopy (Introduction recursion)
【TcaplusDB知识库】TcaplusDB系统用户组介绍
vsomeip3 双机通信文件配置
粒子滤波 PF——在机动目标跟踪中的应用(粒子滤波VS扩展卡尔曼滤波)
Talk about the recent situation of several students from Tsinghua University
AbortController的使用
TCP/IP协议竟然有这么多漏洞?
Unity C # e-learning (IX) -- wwwfrom
HW安全响应
RestCloud ETL抽取动态库表数据实践
夏令营来啦!!!冲冲冲