Skip to content

Commit aca7640

Browse files
Update
1 parent 6c478de commit aca7640

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
|[0098.验证二叉搜索树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0098.验证二叉搜索树.md) ||中等|**递归**|
4545
|[0100.相同的树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0100.相同的树.md) ||简单|**递归** |
4646
|[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.md) ||简单|**递归** **迭代/队列/栈**|
47-
|[0104.二叉树的最大深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0104.二叉树的最大深度.md) ||简单|**递归** **队列/BFS**|
47+
|[0104.二叉树的最大深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0104.二叉树的最大深度.md) ||简单|**递归** **迭代/队列/BFS**|
4848
|[0110.平衡二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0110.平衡二叉树.md) ||简单|**递归**|
4949
|[0111.二叉树的最小深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0111.二叉树的最小深度.md) ||简单|**递归** **队列/BFS**|
5050
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
@@ -73,7 +73,8 @@
7373
|[0575.分糖果](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |简单|**哈希**|
7474
|[0617.合并二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0617.合并二叉树.md) ||简单|**递归** **迭代**|
7575
|[0654.最大二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0654.最大二叉树.md) ||中等|**递归**|
76-
|[0700.二叉搜索树中的搜索](https://github.com/youngyangyang04/leetcode/blob/master/problems/0700.二叉搜索树中的搜索.md) ||简单|**递归**|
76+
|[0700.二叉搜索树中的搜索](https://github.com/youngyangyang04/leetcode/blob/master/problems/0700.二叉搜索树中的搜索.md) ||简单|**递归** **迭代**|
77+
|[0701.二叉搜索树中的插入操作](https://github.com/youngyangyang04/leetcode/blob/master/problems/0701.二叉搜索树中的插入操作.md) ||简单|**递归** **迭代**|
7778
|[0705.设计哈希集合](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|
7879
|[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md) |链表 |中等|**模拟**|
7980
|[1047.删除字符串中的所有相邻重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/1047.删除字符串中的所有相邻重复项.md) ||简单|****|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## 题目地址
2+
https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/
3+
4+
## 思路
5+
6+
7+
## C++代码
8+
9+
### 递归
10+
```
11+
class Solution {
12+
public:
13+
TreeNode* insertIntoBST(TreeNode* root, int val) {
14+
if (root == NULL) {
15+
TreeNode* node = new TreeNode(val);
16+
return node;
17+
}
18+
if (root->val > val) root->left = insertIntoBST(root->left, val);
19+
if (root->val < val) root->right = insertIntoBST(root->right, val);
20+
return root;
21+
}
22+
};
23+
```
24+
25+
### 迭代
26+
27+
```
28+
class Solution {
29+
public:
30+
TreeNode* insertIntoBST(TreeNode* root, int val) {
31+
if (root == NULL) {
32+
TreeNode* node = new TreeNode(val);
33+
return node;
34+
}
35+
TreeNode* cur = root;
36+
TreeNode* parent = root; // 这个很重要,需要记录上一个节点,否则无法赋值新节点
37+
while (cur != NULL) {
38+
parent = cur;
39+
if (cur->val > val) cur = cur->left;
40+
else cur = cur->right;
41+
}
42+
TreeNode* node = new TreeNode(val);
43+
if (val < parent->val) parent->left = node;// 此时是用parent节点的进行赋值
44+
else parent->right = node;
45+
return root;
46+
}
47+
};
48+
```
49+
50+
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。

0 commit comments

Comments
 (0)