当前位置:网站首页>【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
边栏推荐
- 音视频学习(二)——帧率、码流和分辨率
- [file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
- sqlite加载csv文件,并做数据分析
- RestCloud ETL抽取動態庫錶數據實踐
- JS simple deepcopy (Introduction recursion)
- 北京房山区专精特新小巨人企业认定条件,补贴50万
- 2022北京石景山区专精特新中小企业申报流程,补贴10-20万
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
- 评价——模糊综合评价
- 【SNMP】snmp trap 介绍、安装、命令|Trap的发送与接收代码实现
猜你喜欢

【ceph】CephFS 内部实现(四):MDS是如何启动的?--未消化

【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍

数据库-视图

Ansible自动化的运用

IDEA本地代理后,无法下载插件

【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍

【TcaplusDB知识库】TcaplusDB系统管理介绍

【ceph】CephFS 内部实现(三):快照

RestCloud ETL抽取动态库表数据实践

Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)
随机推荐
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
Unity unitywebrequest download package
Unity C # e-learning (10) -- unitywebrequest (2)
【C语言练习——打印空心上三角及其变形】
音视频学习(二)——帧率、码流和分辨率
通过券商经理的开户链接开股票账户安全吗?还是去证券公司开户安全?
【ceph】CephFS 内部实现(二):示例--未消化
数据库-视图
Cache page keepalive use in Vue
JS handwritten bind, apply, call
AbortController的使用
5张图诠释了容器网络
1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)
面试高频 | 你追我赶的Flink双流join
功能:crypto-js加密解密
ETL过程中数据精度不准确问题
English grammar_ Adjective / adverb Level 3 - original sentence pattern
【TcaplusDB知识库】TcaplusDB数据构造介绍
Mr. Du said that the website was updated with illustrations
【TcaplusDB知识库】TcaplusDB运维单据介绍