LeetCode 116. Populating Next Right Pointers in Each Node — go solution
Published in
Jun 29, 2022
func connect(root *Node) *Node {
if root == nil { return root }
var pre, cur *Node
pre = root
for pre.Left != nil {
cur = pre
for cur != nil {
cur.Left.Next = cur.Right
if cur.Next != nil { cur.Right.Next = cur.Next.Left }
cur = cur.Next
}
pre = pre.Left
}
return root
}
node 本身有預設 next 屬性,因此只要由右到左接上,最後的 null 是預設的不用處理