Skip to content

Commit d1e24bf

Browse files
authored
Merge pull request halfrost#165 from 3inchtime/116
add 116 solution
2 parents 6e74af9 + 7b7a392 commit d1e24bf

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package leetcode
2+
3+
type Node struct {
4+
Val int
5+
Left *Node
6+
Right *Node
7+
Next *Node
8+
}
9+
10+
//解法一:迭代
11+
func connect(root *Node) *Node {
12+
if root == nil {
13+
return root
14+
}
15+
16+
q := []*Node{root}
17+
18+
for len(q) > 0 {
19+
var p []*Node
20+
21+
// 遍历这一层的所有节点
22+
for i, node := range q {
23+
if i+1 < len(q) {
24+
node.Next = q[i+1]
25+
}
26+
if node.Left != nil {
27+
p = append(p, node.Left)
28+
}
29+
if node.Right != nil {
30+
p = append(p, node.Right)
31+
}
32+
}
33+
q = p
34+
}
35+
36+
return root
37+
}
38+
39+
// 解法二 递归
40+
func connect2(root *Node) *Node {
41+
if root == nil {
42+
return nil
43+
}
44+
connectTwoNode(root.Left, root.Right)
45+
46+
return root
47+
}
48+
49+
func connectTwoNode(node1, node2 *Node) {
50+
if node1 == nil || node2 == nil {
51+
return
52+
}
53+
node1.Next = node2
54+
55+
connectTwoNode(node1.Left, node1.Right)
56+
connectTwoNode(node2.Left, node2.Right)
57+
connectTwoNode(node1.Right, node2.Left)
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question116 struct {
9+
para116
10+
ans116
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para116 struct {
16+
one *Node
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans116 struct {
22+
one *Node
23+
}
24+
25+
func newQuestionNode()*Node{
26+
node7 := &Node{}
27+
node7.Val = 7
28+
29+
node6 := &Node{}
30+
node6.Val = 6
31+
32+
node5 := &Node{}
33+
node5.Val = 5
34+
35+
node4 := &Node{}
36+
node4.Val = 4
37+
38+
node3 := &Node{}
39+
node3.Val = 3
40+
41+
node2 := &Node{}
42+
node2.Val = 2
43+
44+
node1 := &Node{}
45+
node1.Val = 1
46+
47+
node1.Left = node2
48+
node1.Right = node3
49+
50+
node2.Left = node4
51+
node2.Right = node5
52+
53+
node3.Left = node6
54+
node3.Right = node7
55+
return node1
56+
}
57+
58+
func newResultNode()*Node{
59+
node7 := &Node{}
60+
node7.Val = 7
61+
62+
node6 := &Node{}
63+
node6.Val = 6
64+
65+
node5 := &Node{}
66+
node5.Val = 5
67+
68+
node4 := &Node{}
69+
node4.Val = 4
70+
71+
node3 := &Node{}
72+
node3.Val = 3
73+
74+
node2 := &Node{}
75+
node2.Val = 2
76+
77+
node1 := &Node{}
78+
node1.Val = 1
79+
80+
node1.Left = node2
81+
node1.Right = node3
82+
83+
node2.Left = node4
84+
node2.Right = node5
85+
86+
node3.Left = node6
87+
node3.Right = node7
88+
89+
node1.Next = nil
90+
node2.Next = node3
91+
node3.Next = nil
92+
node4.Next = node5
93+
node5.Next = node6
94+
node6.Next = node7
95+
node7.Next = nil
96+
97+
return node1
98+
}
99+
100+
func Test_Problem116(t *testing.T) {
101+
102+
qs := []question116{
103+
{
104+
para116{newQuestionNode()},
105+
ans116{newResultNode()},
106+
},
107+
}
108+
109+
fmt.Printf("------------------------Leetcode Problem 116------------------------\n")
110+
111+
for _, q := range qs {
112+
_, p := q.ans116, q.para116
113+
fmt.Printf("【input】:%v ", p.one)
114+
fmt.Printf("【output】:%v \n", connect(p.one))
115+
}
116+
fmt.Printf("\n\n\n")
117+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# [116. Distinct Subsequences](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)
2+
3+
4+
## 题目
5+
6+
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
7+
```go
8+
struct Node {
9+
int val;
10+
Node *left;
11+
Node *right;
12+
Node *next;
13+
}
14+
```
15+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
16+
17+
Initially, all next pointers are set to NULL.
18+
19+
**Follow up:**
20+
- You may only use constant extra space.
21+
- Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
22+
23+
24+
**Example 1:**
25+
26+
```
27+
Input: root = [1,2,3,4,5,6,7]
28+
Output: [1,#,2,3,#,4,5,6,7,#]
29+
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
30+
```
31+
32+
**Constraints:**
33+
- The number of nodes in the given tree is less than 4096.
34+
- -1000 <= node.val <= 1000
35+
36+
## 题目大意
37+
38+
将二叉树的每一层节点都连接起来形成一个链表,每层的最后一个节点指向NULL.
39+
40+
41+
## 解题思路
42+
43+
本质上是二叉树的层序遍历,基于广度优先搜索,将每层的节点放入队列,并遍历队列进行连接。
44+
45+
46+
## 代码
47+
48+
```go
49+
package leetcode
50+
51+
// 解法一:迭代
52+
func connect(root *Node) *Node {
53+
if root == nil {
54+
return root
55+
}
56+
57+
q := []*Node{root}
58+
59+
for len(q) > 0 {
60+
var p []*Node
61+
62+
for i, node := range q {
63+
if i+1 < len(q) {
64+
node.Next = q[i+1]
65+
}
66+
if node.Left != nil {
67+
p = append(p, node.Left)
68+
}
69+
if node.Right != nil {
70+
p = append(p, node.Right)
71+
}
72+
}
73+
q = p
74+
}
75+
76+
return root
77+
}
78+
79+
80+
// 解法二 递归
81+
func connect2(root *Node) *Node {
82+
if root == nil {
83+
return nil
84+
}
85+
connectTwoNode(root.Left, root.Right)
86+
87+
return root
88+
}
89+
90+
func connectTwoNode(node1, node2 *Node) {
91+
if node1 == nil || node2 == nil {
92+
return
93+
}
94+
node1.Next = node2
95+
96+
connectTwoNode(node1.Left, node1.Right)
97+
connectTwoNode(node2.Left, node2.Right)
98+
connectTwoNode(node1.Right, node2.Left)
99+
}
100+
```

0 commit comments

Comments
 (0)