当前位置:网站首页>【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
边栏推荐
猜你喜欢

【ceph】mkdir|mksnap流程源码分析|锁状态切换实例

Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)

A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction](/img/66/f3ab0514d691967ad88535ae1448c1.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction

How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth

RestCloud ETL与Kettle对比分析

ETL过程中数据精度不准确问题

评价——模糊综合评价

使用RestCloud ETL Shell组件实现定时调度DataX离线任务

BLE抓包调试信息分析
随机推荐
vsomeip3 双机通信文件配置
数据库-视图
功能:crypto-js加密解密
JS events
【小程序实战系列】小程序框架 页面注册 生命周期 介绍
Using restcloud ETL shell component to schedule dataX offline tasks
【ceph】CephFS 内部实现(三):快照
【TcaplusDB知识库】TcaplusDB常规单据介绍
RestCloud ETL解决shell脚本参数化
/etc/profile、/etc/bashrc、~/.bashrc的区别
Audio and video learning (I) -- PTZ control principle
A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
Advanced operation of MySQL database basic SQL statement tutorial
[CEPH] cephfs internal implementation (II): example -- undigested
Summer camp is coming!!! Chongchongchong
Redis cluster
Lexin AWS IOT expresslink module achieves universal availability
Is it safe to buy stocks and open accounts through the QR code of the securities manager? Want to open an account for stock trading
Analysis of ble packet capturing debugging information
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure