当前位置:网站首页>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);
}
}
}

边栏推荐
- Day-15 common APIs and exception mechanisms
- Several ways to clear floating
- Static vxlan configuration
- test
- 数据库安全的重要性
- The left-hand side of an assignment expression may not be an optional property access. ts(2779)
- ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
- 爱可可AI前沿推介(7.7)
- 对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
- Attack and defense world ----- summary of web knowledge points
猜你喜欢

ICLR 2022 | 基于对抗自注意力机制的预训练语言模型

Static routing assignment of network reachable and telent connections

爱可可AI前沿推介(7.7)

【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移

leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)

Vxlan 静态集中网关

Connect to blog method, overload, recursion

解密GD32 MCU产品家族,开发板该怎么选?

Several ways to clear floating
![[statistical learning method] learning notes - logistic regression and maximum entropy model](/img/f7/857d053cc2cee81c24919aafab3c6e.png)
[statistical learning method] learning notes - logistic regression and maximum entropy model
随机推荐
[difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
How to apply @transactional transaction annotation to perfection?
静态Vxlan 配置
Session
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
Preorder, inorder and postorder traversal of binary tree
【从 0 开始学微服务】【01】什么是微服务
What if does not match your user account appears when submitting the code?
[pytorch practice] image description -- let neural network read pictures and tell stories
数据库安全的重要性
编译 libssl 报错
Day-15 common APIs and exception mechanisms
【统计学习方法】学习笔记——支持向量机(上)
Cookie
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
2022 practice questions and mock examination of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge)
Master公式。(用于计算递归的时间复杂度。)
浅谈估值模型 (二): PE指标II——PE Band
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)