LeetCode 116. Populating Next Right Pointers in Each Node — go solution

Gary Huang
Dublin so code
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 是預設的不用處理

--

--

Gary Huang
Dublin so code

Self-taught in programming, currently working as a web developer and also a online course instructor, helping self-taught programmers find employment.