当前位置:网站首页>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;
}
}
边栏推荐
- Migrate Infones to stm32
- 03. Login of development blog project
- Rustdesk builds its own remote desktop relay server
- 大型网站如何选择比较好的云主机服务商?
- A master in the field of software architecture -- Reading Notes of the beauty of Architecture
- 初识数据库
- LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
- Summary of deep learning tuning tricks
- Zoom through the mouse wheel
- 【torch】|torch. nn. utils. clip_ grad_ norm_
猜你喜欢

(column 22) typical column questions of C language: delete the specified letters in the string.

Self built DNS server, the client opens the web page slowly, the solution

B站刘二大人-Softmx分类器及MNIST实现-Lecture 9

Graduation design game mall

清除浮动的方式

Sequoiadb Lake warehouse integrated distributed database, June 2022 issue

华为BFD的配置规范

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
![[Tang Laoshi] C -- encapsulation: classes and objects](/img/4e/30d2d4652ea2d4cd5fa7cbbb795863.jpg)
[Tang Laoshi] C -- encapsulation: classes and objects

华为路由器如何配置静态路由
随机推荐
ArcGIS应用基础4 专题图的制作
Station B, Master Liu Er - dataset and data loading
局域网同一个网段通信过程
Auto. JS learning notes 17: basic listening events and UI simple click event operations
Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist
Clear floating mode
Implementation of linked list in address book management system
27io stream, byte output stream, OutputStream writes data to file
Classes and objects (I) detailed explanation of this pointer
PDK工艺库安装-CSMC
Solution of QT TCP packet sticking
[experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
[JVM] [Chapter 17] [garbage collector]
改善Jpopup以实现动态控制disable
移植InfoNES到STM32
[detailed explanation of Huawei machine test] statistics of shooting competition results
H3C防火墙RBM+VRRP 组网配置
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
My 2021
JS array list actual use summary