Skip to content

Commit 8730451

Browse files
committed
feature: add go solution for leetcode 0117
1 parent 7e565c5 commit 8730451

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for a Node.
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 nil
14+
}
15+
if root.Left != nil && root.Right != nil {
16+
root.Left.Next = root.Right
17+
}
18+
if root.Left != nil && root.Right == nil {
19+
root.Left.Next = getNext(root.Next)
20+
}
21+
if root.Right != nil {
22+
root.Right.Next = getNext(root.Next)
23+
}
24+
//先连接右侧节点
25+
connect(root.Right)
26+
connect(root.Left)
27+
return root
28+
}
29+
30+
func getNext(node *Node) *Node {
31+
for node != nil {
32+
if node.Left != nil {
33+
return node.Left
34+
}
35+
if node.Right != nil {
36+
return node.Right
37+
}
38+
node = node.Next
39+
}
40+
return nil
41+
}

0 commit comments

Comments
 (0)