Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1623 from tahsintunan/116
Browse files Browse the repository at this point in the history
Create: 116. Populating Next Right Pointers in Each Node
  • Loading branch information
neetcode-gh authored Dec 28, 2022
2 parents 1389c10 + 7084335 commit 75fea9c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions go/0116-populating-next-right-pointers-in-each-node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for a Node.
* type Node struct {
* Val int
* Left *Node
* Right *Node
* Next *Node
* }
*/

func connect(root *Node) *Node {
populate(root)
return root
}

func populate(node *Node) {
if node == nil {
return
}
if node.Left == nil {
return
}

node.Left.Next = node.Right
if node.Next != nil {
node.Right.Next = node.Next.Left
}
populate(node.Left)
populate(node.Right)
}

0 comments on commit 75fea9c

Please sign in to comment.