当前位置:网站首页>Leetcode question brushing: binary tree 26 (insertion operation in binary search tree)
Leetcode question brushing: binary tree 26 (insertion operation in binary search tree)
2022-07-07 12:48:00 【Taotao can't learn English】
701. Insert operation in binary search tree
Given a binary search tree (BST) The root node of and the value to insert into the tree , 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 return any valid result .
Tips :
- The number of nodes on a given tree is between 0 and 10^4 Between
- Each node has a unique integer value , Value range from 0 To 10^8
- -10^8 <= val <= 10^8
- The new value is different from any node value in the original binary search tree
A very simple question , Traverse in the way of binary search tree , When you encounter leaf nodes, you can directly judge the size , Add left or right... As appropriate ; Single output node encountered , First judge the size , If it is a branch without degree , Then add a new left or right node directly on this node , If it is a branch with out degree , Loop to that node and then process .
package com.programmercarl.tree;
/** * @ClassName InsertIntoBST * @Descriotion TODO * @Author nitaotao * @Date 2022/7/6 14:47 * @Version 1.0 * https://leetcode.cn/problems/insert-into-a-binary-search-tree/ * 701. Insert operation in binary search tree **/
public class InsertIntoBST {
public TreeNode insertIntoBST(TreeNode root, int val) {
// Empty root node , Just create a node and return
if (root == null) {
return new TreeNode(val);
}
TreeNode node = root;
insert(node, val);
return root;
}
public void insert(TreeNode root, int val) {
// The current node is a leaf node
if (root.left == null && root.right == null) {
if (root.val > val) {
// The current value is larger than the target value
root.left = new TreeNode(val);
} else {
root.right = new TreeNode(val);
}
return;
}
// Single output
if (root.left == null && root.right != null) {
if (root.val > val) {
// The current value is larger than the target value
root.left = new TreeNode(val);
return;
}
}
if (root.left != null && root.right == null) {
if (root.val < val) {
// The current value is smaller than the target value
root.right = new TreeNode(val);
return;
}
}
if (root.val > val) {
// The current value is larger than the target value
insert(root.left, val);
} else {
insert(root.right, val);
}
}
}
边栏推荐
- SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
- 【从 0 开始学微服务】【03】初探微服务架构
- Talk about four cluster schemes of redis cache, and their advantages and disadvantages
- 广州市召开安全生产工作会议
- 静态Vxlan 配置
- Common knowledge of one-dimensional array and two-dimensional array
- MySQL导入SQL文件及常用命令
- 【统计学习方法】学习笔记——支持向量机(下)
- Attack and defense world ----- summary of web knowledge points
- Experiment with a web server that configures its own content
猜你喜欢
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
leetcode刷题:二叉树24(二叉树的最近公共祖先)
AirServer自动接收多画面投屏或者跨设备投屏
Attack and defense world ----- summary of web knowledge points
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
图形对象的创建与赋值
【统计学习方法】学习笔记——支持向量机(上)
visual stdio 2017关于opencv4.1的环境配置
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
Static vxlan configuration
随机推荐
【二叉树】删点成林
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
Cookie
Polymorphism, final, etc
利用棧來實現二進制轉化為十進制
xshell评估期已过怎么办
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Connect to blog method, overload, recursion
[deep learning] image multi label classification task, Baidu paddleclas
浅谈估值模型 (二): PE指标II——PE Band
【统计学习方法】学习笔记——支持向量机(下)
gcc 编译报错
Minimalist movie website
[爬虫]使用selenium时,躲避脚本检测
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
图像像素读写操作
leetcode刷题:二叉树20(二叉搜索树中的搜索)
opencv的四个函数
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备