当前位置:网站首页>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);
}
}
}
边栏推荐
猜你喜欢
BGP actual network configuration
[爬虫]使用selenium时,躲避脚本检测
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
【统计学习方法】学习笔记——提升方法
Configure an encrypted web server
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
【统计学习方法】学习笔记——第五章:决策树
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
[crawler] avoid script detection when using selenium
随机推荐
IPv6 experiment
The IDM server response shows that you do not have permission to download the solution tutorial
Image pixel read / write operation
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
【PyTorch实战】用RNN写诗
有什么类方法或是函数可以查看某个项目的Laravel版本的?
编译 libssl 报错
[statistical learning method] learning notes - support vector machine (Part 2)
MySQL导入SQL文件及常用命令
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
[difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
3D content generation based on nerf
Airserver automatically receives multi screen projection or cross device projection
[statistical learning method] learning notes - support vector machine (I)
mysql怎么创建,删除,查看索引?
浅谈估值模型 (二): PE指标II——PE Band
leetcode刷题:二叉树21(验证二叉搜索树)
leetcode刷题:二叉树24(二叉树的最近公共祖先)
BGP third experiment report
BGP actual network configuration