当前位置:网站首页>Leetcode 701 insertion operation in binary search tree -- recursive method and iterative method
Leetcode 701 insertion operation in binary search tree -- recursive method and iterative method
2022-07-06 05:46:00 【Hello, I'm Boger】
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/insert-into-a-binary-search-tree
The question :
Given a binary search tree (BST) The root node root And the value to be inserted into the tree value , Insert values into a binary search tree . Return to the root node of the binary search tree after insertion . input data Guarantee , The new value is different from any node value in the original binary search tree .
Be careful , There may be many effective insertion methods , As long as the tree remains a binary search tree after insertion . You can go back to Any valid result .
Example 1:
Input :root = [4,2,7,1,3], val = 5
Output :[4,2,7,1,3,5]
explain : Another tree that meets the requirements of the topic is :
Example 2:
Input :root = [40,20,60,10,30,50,70], val = 25
Output :[40,20,60,10,30,50,70,null,null,25]
Example 3:
Input :root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
Output :[4,2,7,1,3,5]
Tips :
The number of nodes in the tree will be [0, 104] Within the scope of .
-108 <= Node.val <= 108
All worth Node.val yes unique Of .
-108 <= val <= 108
Guarantee val In primitive BST Does not exist in the .
Ideas :
In fact, I don't quite understand why this problem is classified as medium difficulty , Quite a simple question .
As long as you understand the concept of binary search tree , Search from the root node of the binary search tree , Until the location found is an empty node , Let's replace this empty node with a node with the value to be inserted .
We use recursive method and iterative method to solve this problem .
Recursive method Java Code :
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
TreeNode node = new TreeNode(val);
return node;
}
if (root.val > val) root.left = insertIntoBST(root.left, val);
if (root.val < val) root.right = insertIntoBST(root.right, val);
return root;
}
}
Iterative method Java Code :
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) return new TreeNode(val);
TreeNode parent = root;
TreeNode cur = root;
while (cur != null) {
parent = cur;
if (cur.val > val) cur = cur.left;
else if (cur.val < val) cur = cur.right;
}
if (parent.val > val) parent.left = new TreeNode(val);
else if (parent.val < val) parent.right = new TreeNode(val);
return root;
}
}
边栏推荐
- High quality coding tool clion
- The digital economy has broken through the waves. Is Ltd a Web3.0 website with independent rights and interests?
- Problems encountered in installing mysql8 on MAC
- 大型网站如何选择比较好的云主机服务商?
- 无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
- Detailed summary of SQL injection
- How to use PHP string query function
- Easy to understand IIC protocol explanation
- [QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
- Station B Liu Erden softmx classifier and MNIST implementation -structure 9
猜你喜欢

Sequoiadb Lake warehouse integrated distributed database, June 2022 issue

Redis message queue

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

B站刘二大人-数据集及数据加载 Lecture 8

05. Security of blog project

Station B, Master Liu Er - dataset and data loading

03. 开发博客项目之登录

Winter 2021 pat class B problem solution (C language)

初识数据库

自建DNS服务器,客户端打开网页慢,解决办法
随机推荐
PDK工艺库安装-CSMC
59. Spiral matrix
27io stream, byte output stream, OutputStream writes data to file
First acquaintance with CDN
26file filter anonymous inner class and lambda optimization
PDK process library installation -csmc
PDK工藝庫安裝-CSMC
Text classification still stays at Bert? The dual contrast learning framework is too strong
H3C防火墙RBM+VRRP 组网配置
CoDeSys note 2: set coil and reset coil
Algorithm -- climbing stairs (kotlin)
网络协议模型
First knowledge database
js Array 列表 实战使用总结
The digital economy has broken through the waves. Is Ltd a Web3.0 website with independent rights and interests?
Summary of data sets in intrusion detection field
ArcGIS application foundation 4 thematic map making
[SQL Server fast track] - authentication and establishment and management of user accounts
Web Security (VI) the use of session and the difference between session and cookie
What impact will frequent job hopping have on your career?