当前位置:网站首页>leetcode:701. Insertion in binary search tree [BST insertion]
leetcode:701. Insertion in binary search tree [BST insertion]
2022-07-03 01:06:00 【White speed Dragon King's review】
analysis
Be careful ,bst Insert directly into leaves
There is no need to refactor like delete
This needs attention
ac code
# 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:
# Insert directly into the leaf without refactoring
if not root: return TreeNode(val)
def dfs(node):
if val <= node.val:
# Insert leaves directly
if not node.left:
node.left = TreeNode(val)
# Put it on the next floor
else:
dfs(node.left)
else:
if not node.right:
node.right = TreeNode(val)
else:
dfs(node.right)
dfs(root)
return root
summary
bst Insert common sense
边栏推荐
猜你喜欢
【AutoSAR 八 OS】
leetcode-849:到最近的人的最大距离
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
Key detection and sinusoidal signal output developed by Arduino
[love crash] neglected details of gibaro
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
[AUTOSAR I overview]
Embrace the safety concept of platform delivery
【爱死机】《吉巴罗》被忽略的细节
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
随机推荐
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
关于Fibonacci数列
KingbaseES ALTER TABLE 中 USING 子句的用法
有向图的强连通分量
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
Win10 can't be installed in many ways Problems with NET3.5
Leetcode-871: minimum refueling times
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
How to systematically learn machine learning
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
【案例分享】让新时代教育发展与“数”俱进
【爱死机】《吉巴罗》被忽略的细节
【AutoSAR 十三 NVM】
【AutoSAR 十二 模式管理】
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
数学建模之线性规划(含MATLAB代码)
leetcode-241:为运算表达式设计优先级
tail -f 、tail -F、tailf的区别
12_微信小程序之微信视频号滚动自动播放视频效果实现