File tree 1 file changed +41
-0
lines changed
solution/0100-0199/0117.Populating Next Right Pointers in Each Node II
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments