当前位置:网站首页>【leetcode】701. Insert operation in binary search tree
【leetcode】701. Insert operation in binary search tree
2022-06-26 15:37:00 【liiiiiiiiiiiiike】
For details, please refer to 701. Insert operation in binary search tree
Their thinking
- BST characteristic , Judge root.val and val Size ,root.val < val, explain val Need to put root.right
- conversely Put it in root.left, Judge root.right Whether there is , If there is a direct release , Otherwise, recursion insert function , Find the right place
# 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
边栏推荐
- [C language practice - printing hollow upper triangle and its deformation]
- Restcloud ETL extracting dynamic library table data
- nanoPi Duo2连接wifi
- Audio and video learning (I) -- PTZ control principle
- 【微信小程序】事件绑定,你搞懂了吗?
- 数据库-序列
- CNN优化trick
- 【问题解决】新版webots纹理等资源文件加载/下载时间过长
- MongoDB系列之适用场景和不适用场景
- 【TcaplusDB知识库】TcaplusDB系统管理介绍
猜你喜欢

Audio and video learning (I) -- PTZ control principle

一篇博客彻底掌握:粒子滤波 particle filter (PF) 的理论及实践(matlab版)

【ceph】CEPHFS 内部实现(一):概念篇--未消化

Unable to download Plug-in after idea local agent
![[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

Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved

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

English grammar_ Adjective / adverb Level 3 - original sentence pattern

Binding method of multiple sub control signal slots under QT

在重新格式化时不要删除自定义换行符(Don‘t remove custom line breaks on reformat)
随机推荐
【ceph】CephFS 内部实现(四):MDS是如何启动的?--未消化
在重新格式化时不要删除自定义换行符(Don‘t remove custom line breaks on reformat)
JS events
[tcapulusdb knowledge base] Introduction to tcapulusdb general documents
功能:crypto-js加密解密
一篇博客彻底掌握:粒子滤波 particle filter (PF) 的理论及实践(matlab版)
Applet: uniapp solves vendor JS is too large
/etc/profile、/etc/bashrc、~/.bashrc的区别
【leetcode】112. 路径总和 - 113. 路径总和 II
Audio and video learning (III) -- SIP protocol
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
Vsomeip3 dual computer communication file configuration
Inaccurate data accuracy in ETL process
5张图诠释了容器网络
一键安装gcc脚本
A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
Sorted out a batch of script standard function modules (version 2021)
在校生学习生涯总结(2022)
【TcaplusDB知识库】TcaplusDB常规单据介绍
Unity C # e-learning (10) -- unitywebrequest (2)