当前位置:网站首页>【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
边栏推荐
- Advanced operation of MySQL database basic SQL statement tutorial
- Inaccurate data accuracy in ETL process
- 【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍
- Using restcloud ETL shell component to schedule dataX offline tasks
- 1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)
- IDEA本地代理后,无法下载插件
- MySQL数据库基本SQL语句教程之高级操作
- Restcloud ETL extraction de données de table de base de données dynamique
- 使用RestCloud ETL Shell组件实现定时调度DataX离线任务
- TCP/IP协议竟然有这么多漏洞?
猜你喜欢

Evaluate:huggingface detailed introduction to the evaluation index module

在重新格式化时不要删除自定义换行符(Don‘t remove custom line breaks on reformat)

【毕业季·进击的技术er】 什么是微信小程序,带你推开小程序的大门
![[CEPH] cephfs internal implementation (II): example -- undigested](/img/87/6eb214550faf1f0500565c1610ff3b.png)
[CEPH] cephfs internal implementation (II): example -- undigested

小程序:uniapp解决 vendor.js 体积过大的问题
![[wechat applet] event binding, do you understand?](/img/83/6242e972538d0423fd4155140bb521.png)
[wechat applet] event binding, do you understand?
![[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested](/img/f9/8e2b55a33e1613a49e08e119ae0086.png)
[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested

查词翻译类应用使用数据接口api总结

Restcloud ETL resolves shell script parameterization

【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍
随机推荐
夏令营来啦!!!冲冲冲
Summary of students' learning career (2022)
Cache page keepalive use in Vue
nanoPi Duo2连接wifi
【毕业季·进击的技术er】 什么是微信小程序,带你推开小程序的大门
/etc/profile、/etc/bashrc、~/. Bashrc differences
PHP file upload 00 truncation
On which platform is it safe to buy shares and open an account? Ask for guidance
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure
1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)
2022 Beijing Shijingshan District specializes in the application process for special new small and medium-sized enterprises, with a subsidy of 100000-200000 yuan
【ceph】CephFS 内部实现(三):快照
MongoDB系列之适用场景和不适用场景
10 minutes to understand bim+gis fusion, common BIM data formats and characteristics
粒子滤波 PF——在机动目标跟踪中的应用(粒子滤波VS扩展卡尔曼滤波)
el-dialog拖拽,边界问题完全修正,网上版本的bug修复
IDEA本地代理后,无法下载插件
JS之事件
JS之简易deepCopy(简介递归)
HR export data Excel VBA