当前位置:网站首页>【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
边栏推荐
- 功能:crypto-js加密解密
- Unity C # e-learning (10) -- unitywebrequest (1)
- 【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
- TCP/IP协议竟然有这么多漏洞?
- Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)
- 1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)
- Unity unitywebrequest download package
- 北京房山区专精特新小巨人企业认定条件,补贴50万
- 有Cmake的工程交叉编译到链接时报错找不到.so动态库文件
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
猜你喜欢

反射修改final
![[CEPH] MKDIR | mksnap process source code analysis | lock state switching example](/img/4a/0aeb69ae6527c65a67be535828b48a.jpg)
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example

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

Lexin AWS IOT expresslink module achieves universal availability

【TcaplusDB知识库】TcaplusDB运维单据介绍

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

English语法_形容词/副词3级 - 原级句型

AbortController的使用
![[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested](/img/f9/8e2b55a33e1613a49e08e119ae0086.png)
[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested

RestCloud ETL抽取動態庫錶數據實踐
随机推荐
Notes on brushing questions (19) -- binary tree: modification and construction of binary search tree
AbortController的使用
AbortController的使用
How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
One click analysis hardware /io/ national network performance script (strong push)
【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
Using restcloud ETL shell component to schedule dataX offline tasks
Is it safe to open a stock account through the account opening link of the broker manager? Or is it safe to open an account in a securities company?
HW安全响应
[graduation season · advanced technology Er] what is a wechat applet, which will help you open the door of the applet
1.会计基础--会计的几大要素(会计总论、会计科目和账户)
Seurat to h5ad summary
数据库-视图
# 粒子滤波 PF——三维匀速运动CV目标跟踪(粒子滤波VS扩展卡尔曼滤波)
Summary of students' learning career (2022)
学习内存屏障
刷题笔记(十九)--二叉树:二叉搜索树的修改与构造
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
【TcaplusDB知识库】TcaplusDB系统用户组介绍