当前位置:网站首页>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;
}
}
边栏推荐
- 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
- [imgui] unity MenuItem shortcut key
- Note the various data set acquisition methods of jvxetable
- Sword finger offer II 039 Maximum rectangular area of histogram
- Web Security (VI) the use of session and the difference between session and cookie
- Pytorch代码注意的细节,容易敲错的地方
- 网站进行服务器迁移前应做好哪些准备?
- Auto. JS learning notes 17: basic listening events and UI simple click event operations
- 自建DNS服务器,客户端打开网页慢,解决办法
- Jvxetable implant j-popup with slot
猜你喜欢
Processes and threads
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
Game push image / table /cv/nlp, multi-threaded start
js Array 列表 实战使用总结
Jushan database appears again in the gold fair to jointly build a new era of digital economy
ArcGIS application foundation 4 thematic map making
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站刘二大人-线性回归 Pytorch
B站刘二大人-线性回归及梯度下降
随机推荐
华为路由器如何配置静态路由
After the project is released, index Html is cached
Embedded interview questions (I: process and thread)
网站进行服务器迁移前应做好哪些准备?
Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
Self built DNS server, the client opens the web page slowly, the solution
【torch】|torch. nn. utils. clip_ grad_ norm_
02. Develop data storage of blog project
03. Login of development blog project
Pay attention to the details of pytoch code, and it is easy to make mistakes
通讯录管理系统链表实现
B站刘二大人-多元逻辑回归 Lecture 7
数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
授予渔,从0开始搭建一个自己想要的网页
Practice sharing: how to safely and quickly migrate from CentOS to openeuler
【经验】UltralSO制作启动盘时报错:磁盘/映像容量太小
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
JS array list actual use summary
华为路由器忘记密码怎么恢复
Station B Liu Erden softmx classifier and MNIST implementation -structure 9