当前位置:网站首页>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);
}
}
}
边栏推荐
猜你喜欢
[deep learning] image multi label classification task, Baidu paddleclas
Routing strategy of multi-point republication [Huawei]
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
Connect to blog method, overload, recursion
What if does not match your user account appears when submitting the code?
基于NeRF的三维内容生成
【深度学习】图像多标签分类任务,百度PaddleClas
[pytorch practice] use pytorch to realize image style migration based on neural network
leetcode刷题:二叉树21(验证二叉搜索树)
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
随机推荐
Configure an encrypted web server
RHSA first day operation
Polymorphism, final, etc
利用栈来实现二进制转化为十进制
OSPF exercise Report
How to apply @transactional transaction annotation to perfection?
SQL Lab (46~53) (continuous update later) order by injection
Image pixel read / write operation
【从 0 开始学微服务】【03】初探微服务架构
Day-19 IO stream
gcc 编译报错
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
IPv6 experiment
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
PHP调用纯真IP数据库返回具体地址
Object. Simple implementation of assign()
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
[statistical learning methods] learning notes - Chapter 5: Decision Tree