当前位置:网站首页>【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
边栏推荐
- 一键分析硬件/IO/全国网络性能脚本(强推)
- 【问题解决】新版webots纹理等资源文件加载/下载时间过长
- [CEPH] Introduction to cephfs caps
- 评价——模糊综合评价
- Mongodb series window environment deployment configuration
- Smoothing data using convolution
- Vsomeip3 dual computer communication file configuration
- Inaccurate data accuracy in ETL process
- On which platform is it safe to buy shares and open an account? Ask for guidance
- /etc/profile、/etc/bashrc、~/.bashrc的区别
猜你喜欢

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

【ceph】CephFS 内部实现(四):MDS是如何启动的?--未消化
![[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction

Using restcloud ETL shell component to schedule dataX offline tasks

el-dialog拖拽,边界问题完全修正,网上版本的bug修复
![[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
![[tcapulusdb knowledge base] Introduction to tcapulusdb system management](/img/5a/28aaf8b115cbf4798cf0b201e4c068.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb system management

【ceph】cephfs caps简介

【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍

Function: crypto JS encryption and decryption
随机推荐
Redis cluster
Applicable and inapplicable scenarios of mongodb series
There are so many vulnerabilities in tcp/ip protocol?
Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
RestCloud ETL与Kettle对比分析
Restcloud ETL resolves shell script parameterization
Unity C # e-learning (VIII) -- www
/etc/profile、/etc/bashrc、~/. Bashrc differences
有Cmake的工程交叉编译到链接时报错找不到.so动态库文件
[CEPH] Introduction to cephfs caps
学习内存屏障
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?
MongoDB系列之适用场景和不适用场景
Learning memory barrier
面试高频 | 你追我赶的Flink双流join
Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)
JS之事件
【TcaplusDB知识库】TcaplusDB数据构造介绍
RestCloud ETL抽取动态库表数据实践
Comparative analysis of restcloud ETL and kettle