当前位置:网站首页>Recursive method to realize the insertion operation in binary search tree
Recursive method to realize the insertion operation in binary search tree
2022-07-06 00:51:00 【Hydrion-Qlz】
701. Insert operation in binary search tree
difficulty : secondary
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 .
Ideas
towards BST When inserting a value in, you need to determine the size of the inserted value and the value of the current node
- If the insertion value is less than the value of the current node , Then find the right position in the left subtree of the current node
- If the insertion value is greater than the value of the current node , Then find a suitable position in the right subtree of the current node
If the code of solution one below is difficult to understand , You can look at the code of solution 2 first
Solution 1 :
package cn.edu.xjtu.carlWay.tree.insertIntoBST;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 701. Insert operation in binary search tree * 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 . * <p> * 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 . * <p> * https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/ */
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) // If the current node is empty , That means val Found the right place , At this point, the created node returns directly to .
return new TreeNode(val);
if (root.val < val){
root.right = insertIntoBST(root.right, val); // Recursively create the right subtree
}else if (root.val > val){
root.left = insertIntoBST(root.left, val); // Recursively create left subtree
}
return root;
}
}
Solution 2 :
If the left child node is empty, insert , If it's not empty, keep looking , The same goes for the right side
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}
if (root.val > val) {
if (root.left == null) {
root.left = new TreeNode(val);
} else {
insertIntoBST(root.left, val);
}
} else {
if (root.right == null) {
root.right = new TreeNode(val);
} else {
insertIntoBST(root.right, val);
}
}
return root;
}
边栏推荐
- 云导DNS和知识科普以及课堂笔记
- 【文件IO的简单实现】
- 免费的聊天机器人API
- Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
- CTF daily question day44 rot
- Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
- Starting from 1.5, build a micro Service Framework - call chain tracking traceid
- OpenCV经典100题
- Spark AQE
- golang mqtt/stomp/nats/amqp
猜你喜欢
Building core knowledge points
关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
BiShe - College Student Association Management System Based on SSM
Leetcode 450 deleting nodes in a binary search tree
Cannot resolve symbol error
MySQL storage engine
随机推荐
curlpost-php
[simple implementation of file IO]
Keepalive component cache does not take effect
An understanding of & array names
BiShe - College Student Association Management System Based on SSM
Lone brave man
logstash清除sincedb_path上传记录,重传日志数据
Idea远程提交spark任务到yarn集群
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
Problems and solutions of converting date into specified string in date class
Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
How spark gets columns in dataframe --column, $, column, apply
Folding and sinking sand -- weekly record of ETF
Cf:c. the third problem
Spark AQE
Ffmpeg captures RTSP images for image analysis
Zhuhai laboratory ventilation system construction and installation instructions
vSphere实现虚拟机迁移
看抖音直播Beyond演唱会有感
Kotlin core programming - algebraic data types and pattern matching (3)