Skip to content

Commit 1eac649

Browse files
committed
Add solution 623
1 parent 2e7dd97 commit 1eac649

26 files changed

+626
-289
lines changed

README.md

Lines changed: 219 additions & 219 deletions
Large diffs are not rendered by default.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/halfrost/LeetCode-Go/structures"
5+
)
6+
7+
// TreeNode define
8+
type TreeNode = structures.TreeNode
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
func addOneRow(root *TreeNode, v int, d int) *TreeNode {
19+
if d == 1 {
20+
tmp := &TreeNode{Val: v, Left: root, Right: nil}
21+
return tmp
22+
}
23+
level := 1
24+
addTreeRow(root, v, d, &level)
25+
return root
26+
}
27+
28+
func addTreeRow(root *TreeNode, v, d int, currLevel *int) {
29+
if *currLevel == d-1 {
30+
root.Left = &TreeNode{Val: v, Left: root.Left, Right: nil}
31+
root.Right = &TreeNode{Val: v, Left: nil, Right: root.Right}
32+
return
33+
}
34+
*currLevel++
35+
if root.Left != nil {
36+
addTreeRow(root.Left, v, d, currLevel)
37+
}
38+
if root.Right != nil {
39+
addTreeRow(root.Right, v, d, currLevel)
40+
}
41+
*currLevel--
42+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question623 struct {
11+
para623
12+
ans623
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para623 struct {
18+
one []int
19+
v int
20+
d int
21+
}
22+
23+
// ans 是答案
24+
// one 代表第一个答案
25+
type ans623 struct {
26+
one []int
27+
}
28+
29+
func Test_Problem623(t *testing.T) {
30+
31+
qs := []question623{
32+
33+
{
34+
para623{[]int{4, 2, 6, 3, 1, 5, structures.NULL}, 1, 2},
35+
ans623{[]int{4, 1, 1, 2, structures.NULL, structures.NULL, 6, 3, 1, 5, structures.NULL}},
36+
},
37+
38+
{
39+
para623{[]int{4, 2, structures.NULL, 3, 1}, 1, 3},
40+
ans623{[]int{4, 2, structures.NULL, 1, 1, 3, structures.NULL, structures.NULL, 1}},
41+
},
42+
43+
{
44+
para623{[]int{1, 2, 3, 4}, 5, 4},
45+
ans623{[]int{1, 2, 3, 4, structures.NULL, structures.NULL, structures.NULL, 5, 5}},
46+
},
47+
48+
{
49+
para623{[]int{4, 2, 6, 3, 1, 5}, 1, 3},
50+
ans623{[]int{4, 2, 6, 1, 1, 1, 1, 3, structures.NULL, structures.NULL, 1, 5}},
51+
},
52+
53+
{
54+
para623{[]int{4, 2, 6, 3, 1, 5}, 1, 1},
55+
ans623{[]int{1, 4, structures.NULL, 2, 6, 3, 1, 5}},
56+
},
57+
}
58+
59+
fmt.Printf("------------------------Leetcode Problem 623------------------------\n")
60+
61+
for _, q := range qs {
62+
_, p := q.ans623, q.para623
63+
fmt.Printf("【input】:%v ", p)
64+
root := structures.Ints2TreeNode(p.one)
65+
fmt.Printf("【output】:%v \n", structures.Tree2Preorder(addOneRow(root, p.v, p.d)))
66+
}
67+
fmt.Printf("\n\n\n")
68+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# [623. Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)
2+
3+
4+
## 题目
5+
6+
Given the root of a binary tree, then value `v` and depth `d`, you need to add a row of nodes with value `v` at the given depth `d`. The root node is at depth 1.
7+
8+
The adding rule is: given a positive integer depth `d`, for each NOT null tree nodes `N` in depth `d-1`, create two tree nodes with value `v` as `N's` left subtree root and right subtree root. And `N's` **original left subtree** should be the left subtree of the new left subtree root, its **original right subtree** should be the right subtree of the new right subtree root. If depth `d` is 1 that means there is no depth d-1 at all, then create a tree node with value **v** as the new root of the whole original tree, and the original tree is the new root's left subtree.
9+
10+
**Example 1:**
11+
12+
```
13+
Input:
14+
A binary tree as following:
15+
4
16+
/ \
17+
2 6
18+
/ \ /
19+
3 1 5
20+
21+
v = 1d = 2Output:
22+
4
23+
/ \
24+
1 1
25+
/ \
26+
2 6
27+
/ \ /
28+
3 1 5
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input:
35+
A binary tree as following:
36+
4
37+
/
38+
2
39+
/ \
40+
3 1
41+
42+
v = 1d = 3Output:
43+
4
44+
/
45+
2
46+
/ \
47+
1 1
48+
/ \
49+
3 1
50+
```
51+
52+
**Note:**
53+
54+
1. The given d is in range [1, maximum depth of the given tree + 1].
55+
2. The given binary tree has at least one tree node.
56+
57+
## 题目大意
58+
59+
给定一个二叉树,根节点为第1层,深度为 1。在其第 d 层追加一行值为 v 的节点。添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N,为 N 创建两个值为 v 的左子树和右子树。将 N 原先的左子树,连接为新节点 v 的左子树;将 N 原先的右子树,连接为新节点 v 的右子树。如果 d 的值为 1,深度 d - 1 不存在,则创建一个新的根节点 v,原先的整棵树将作为 v 的左子树。
60+
61+
## 解题思路
62+
63+
- 这一题虽然是 Medium,实际非常简单。给二叉树添加一行,用 DFS 或者 BFS,遍历过程中记录行数,到达目标行一行,增加节点即可。不过需要注意 2 个特殊情况,特殊情况一,`d==1`,此时需要添加的行即为根节点。特殊情况二,`d>height(root)`,即要添加的行数比树还要高,这时只需要在最下层的叶子节点添加一层。时间复杂度 O(n),空间复杂度 O(n)。
64+
65+
## 代码
66+
67+
```go
68+
package leetcode
69+
70+
import (
71+
"github.com/halfrost/LeetCode-Go/structures"
72+
)
73+
74+
// TreeNode define
75+
type TreeNode = structures.TreeNode
76+
77+
/**
78+
* Definition for a binary tree node.
79+
* type TreeNode struct {
80+
* Val int
81+
* Left *TreeNode
82+
* Right *TreeNode
83+
* }
84+
*/
85+
func addOneRow(root *TreeNode, v int, d int) *TreeNode {
86+
if d == 1 {
87+
tmp := &TreeNode{Val: v, Left: root, Right: nil}
88+
return tmp
89+
}
90+
level := 1
91+
addTreeRow(root, v, d, &level)
92+
return root
93+
}
94+
95+
func addTreeRow(root *TreeNode, v, d int, currLevel *int) {
96+
if *currLevel == d-1 {
97+
root.Left = &TreeNode{Val: v, Left: root.Left, Right: nil}
98+
root.Right = &TreeNode{Val: v, Left: nil, Right: root.Right}
99+
return
100+
}
101+
*currLevel++
102+
if root.Left != nil {
103+
addTreeRow(root.Left, v, d, currLevel)
104+
}
105+
if root.Right != nil {
106+
addTreeRow(root.Right, v, d, currLevel)
107+
}
108+
*currLevel--
109+
}
110+
```

website/content/ChapterFour/0600~0699/0605.Can-Place-Flowers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ func canPlaceFlowers(flowerbed []int, n int) bool {
6363
----------------------------------------------
6464
<div style="display: flex;justify-content: space-between;align-items: center;">
6565
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0599.Minimum-Index-Sum-of-Two-Lists/">⬅️上一页</a></p>
66-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0628.Maximum-Product-of-Three-Numbers/">下一页➡️</a></p>
66+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0623.Add-One-Row-to-Tree/">下一页➡️</a></p>
6767
</div>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# [623. Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)
2+
3+
4+
## 题目
5+
6+
Given the root of a binary tree, then value `v` and depth `d`, you need to add a row of nodes with value `v` at the given depth `d`. The root node is at depth 1.
7+
8+
The adding rule is: given a positive integer depth `d`, for each NOT null tree nodes `N` in depth `d-1`, create two tree nodes with value `v` as `N's` left subtree root and right subtree root. And `N's` **original left subtree** should be the left subtree of the new left subtree root, its **original right subtree** should be the right subtree of the new right subtree root. If depth `d` is 1 that means there is no depth d-1 at all, then create a tree node with value **v** as the new root of the whole original tree, and the original tree is the new root's left subtree.
9+
10+
**Example 1:**
11+
12+
```
13+
Input:
14+
A binary tree as following:
15+
4
16+
/ \
17+
2 6
18+
/ \ /
19+
3 1 5
20+
21+
v = 1d = 2Output:
22+
4
23+
/ \
24+
1 1
25+
/ \
26+
2 6
27+
/ \ /
28+
3 1 5
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input:
35+
A binary tree as following:
36+
4
37+
/
38+
2
39+
/ \
40+
3 1
41+
42+
v = 1d = 3Output:
43+
4
44+
/
45+
2
46+
/ \
47+
1 1
48+
/ \
49+
3 1
50+
```
51+
52+
**Note:**
53+
54+
1. The given d is in range [1, maximum depth of the given tree + 1].
55+
2. The given binary tree has at least one tree node.
56+
57+
## 题目大意
58+
59+
给定一个二叉树,根节点为第1层,深度为 1。在其第 d 层追加一行值为 v 的节点。添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N,为 N 创建两个值为 v 的左子树和右子树。将 N 原先的左子树,连接为新节点 v 的左子树;将 N 原先的右子树,连接为新节点 v 的右子树。如果 d 的值为 1,深度 d - 1 不存在,则创建一个新的根节点 v,原先的整棵树将作为 v 的左子树。
60+
61+
## 解题思路
62+
63+
- 这一题虽然是 Medium,实际非常简单。给二叉树添加一行,用 DFS 或者 BFS,遍历过程中记录行数,到达目标行一行,增加节点即可。不过需要注意 2 个特殊情况,特殊情况一,`d==1`,此时需要添加的行即为根节点。特殊情况二,`d>height(root)`,即要添加的行数比树还要高,这时只需要在最下层的叶子节点添加一层。时间复杂度 O(n),空间复杂度 O(n)。
64+
65+
## 代码
66+
67+
```go
68+
package leetcode
69+
70+
import (
71+
"github.com/halfrost/LeetCode-Go/structures"
72+
)
73+
74+
// TreeNode define
75+
type TreeNode = structures.TreeNode
76+
77+
/**
78+
* Definition for a binary tree node.
79+
* type TreeNode struct {
80+
* Val int
81+
* Left *TreeNode
82+
* Right *TreeNode
83+
* }
84+
*/
85+
func addOneRow(root *TreeNode, v int, d int) *TreeNode {
86+
if d == 1 {
87+
tmp := &TreeNode{Val: v, Left: root, Right: nil}
88+
return tmp
89+
}
90+
level := 1
91+
addTreeRow(root, v, d, &level)
92+
return root
93+
}
94+
95+
func addTreeRow(root *TreeNode, v, d int, currLevel *int) {
96+
if *currLevel == d-1 {
97+
root.Left = &TreeNode{Val: v, Left: root.Left, Right: nil}
98+
root.Right = &TreeNode{Val: v, Left: nil, Right: root.Right}
99+
return
100+
}
101+
*currLevel++
102+
if root.Left != nil {
103+
addTreeRow(root.Left, v, d, currLevel)
104+
}
105+
if root.Right != nil {
106+
addTreeRow(root.Right, v, d, currLevel)
107+
}
108+
*currLevel--
109+
}
110+
```
111+
112+
113+
----------------------------------------------
114+
<div style="display: flex;justify-content: space-between;align-items: center;">
115+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0605.Can-Place-Flowers/">⬅️上一页</a></p>
116+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0628.Maximum-Product-of-Three-Numbers/">下一页➡️</a></p>
117+
</div>

website/content/ChapterFour/0600~0699/0628.Maximum-Product-of-Three-Numbers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ func maximumProduct1(nums []int) int {
105105

106106
----------------------------------------------
107107
<div style="display: flex;justify-content: space-between;align-items: center;">
108-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0605.Can-Place-Flowers/">⬅️上一页</a></p>
108+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0623.Add-One-Row-to-Tree/">⬅️上一页</a></p>
109109
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0632.Smallest-Range-Covering-Elements-from-K-Lists/">下一页➡️</a></p>
110110
</div>

0 commit comments

Comments
 (0)