Skip to content

Commit 172d2fc

Browse files
committed
Add solution 701
1 parent 5e2df33 commit 172d2fc

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
import "github.com/halfrost/LeetCode-Go/structures"
4+
5+
// TreeNode define
6+
type TreeNode = structures.TreeNode
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func insert(n *TreeNode, val int) *TreeNode {
17+
if n == nil {
18+
return &TreeNode{Val: val}
19+
}
20+
if n.Val < val {
21+
n.Right = insert(n.Right, val)
22+
} else {
23+
n.Left = insert(n.Left, val)
24+
}
25+
return n
26+
}
27+
28+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
29+
return insert(root, val)
30+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question701 struct {
11+
para701
12+
ans701
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para701 struct {
18+
root []int
19+
val int
20+
}
21+
22+
// ans 是答案
23+
// one 代表第一个答案
24+
type ans701 struct {
25+
one []int
26+
}
27+
28+
func Test_Problem701(t *testing.T) {
29+
30+
qs := []question701{
31+
32+
{
33+
para701{[]int{4, 2, 7, 1, 3}, 5},
34+
ans701{[]int{4, 2, 7, 1, 3, 5}},
35+
},
36+
37+
{
38+
para701{[]int{40, 20, 60, 10, 30, 50, 70}, 25},
39+
ans701{[]int{40, 20, 60, 10, 30, 50, 70, structures.NULL, structures.NULL, 25}},
40+
},
41+
42+
{
43+
para701{[]int{4, 2, 7, 1, 3, structures.NULL, structures.NULL, structures.NULL, structures.NULL, structures.NULL, structures.NULL}, 5},
44+
ans701{[]int{4, 2, 7, 1, 3, 5}},
45+
},
46+
}
47+
48+
fmt.Printf("------------------------Leetcode Problem 701------------------------\n")
49+
50+
for _, q := range qs {
51+
_, p := q.ans701, q.para701
52+
fmt.Printf("【input】:%v ", p)
53+
rootOne := structures.Ints2TreeNode(p.root)
54+
fmt.Printf("【output】:%v \n", structures.Tree2ints(insertIntoBST(rootOne, p.val)))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [701. Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/)
2+
3+
4+
## 题目
5+
6+
You are given the `root` node of a binary search tree (BST) and a `value` to insert into the tree. Return *the root node of the BST after the insertion*. It is **guaranteed** that the new value does not exist in the original BST.
7+
8+
**Notice** that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return **any of them**.
9+
10+
**Example 1:**
11+
12+
![https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg](https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg)
13+
14+
```
15+
Input: root = [4,2,7,1,3], val = 5
16+
Output: [4,2,7,1,3,5]
17+
Explanation: Another accepted tree is:
18+
19+
```
20+
21+
![https://assets.leetcode.com/uploads/2020/10/05/bst.jpg](https://assets.leetcode.com/uploads/2020/10/05/bst.jpg)
22+
23+
**Example 2:**
24+
25+
```
26+
Input: root = [40,20,60,10,30,50,70], val = 25
27+
Output: [40,20,60,10,30,50,70,null,null,25]
28+
29+
```
30+
31+
**Example 3:**
32+
33+
```
34+
Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
35+
Output: [4,2,7,1,3,5]
36+
37+
```
38+
39+
**Constraints:**
40+
41+
- The number of nodes in the tree will be in the range `[0, 104]`.
42+
- `108 <= Node.val <= 108`
43+
- All the values `Node.val` are **unique**.
44+
- `108 <= val <= 108`
45+
- It's **guaranteed** that `val` does not exist in the original BST.
46+
47+
## 题目大意
48+
49+
给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。
50+
51+
## 解题思路
52+
53+
- 简单题。插入节点的方法有多种,笔者这里选择一种简单的方法。从根开始遍历这个二叉树,当前节点的值比待插入节点的值小,则往右遍历;当前节点的值比待插入节点的值大,则往左遍历。最后遍历到空节点便是要插入的地方。
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
import "github.com/halfrost/LeetCode-Go/structures"
61+
62+
// TreeNode define
63+
type TreeNode = structures.TreeNode
64+
65+
/**
66+
* Definition for a binary tree node.
67+
* type TreeNode struct {
68+
* Val int
69+
* Left *TreeNode
70+
* Right *TreeNode
71+
* }
72+
*/
73+
func insert(n *TreeNode, val int) *TreeNode {
74+
if n == nil {
75+
return &TreeNode{Val: val}
76+
}
77+
if n.Val < val {
78+
n.Right = insert(n.Right, val)
79+
} else {
80+
n.Left = insert(n.Left, val)
81+
}
82+
return n
83+
}
84+
85+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
86+
return insert(root, val)
87+
}
88+
```

0 commit comments

Comments
 (0)