35 lines
631 B
Go
35 lines
631 B
Go
// Package q117 implements a solution for https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
|
|
package q117
|
|
|
|
type Node struct {
|
|
Val int
|
|
Left *Node
|
|
Right *Node
|
|
Next *Node
|
|
}
|
|
|
|
func walk(node *Node, depth int, depths *[]*Node) {
|
|
if node == nil {
|
|
return
|
|
}
|
|
|
|
if depth > len(*depths) {
|
|
*depths = append(*depths, nil)
|
|
}
|
|
if (*depths)[depth-1] != nil {
|
|
(*depths)[depth-1].Next = node
|
|
}
|
|
(*depths)[depth-1] = node
|
|
|
|
walk(node.Left, depth+1, depths)
|
|
walk(node.Right, depth+1, depths)
|
|
}
|
|
|
|
func connect(root *Node) *Node {
|
|
depths := []*Node{}
|
|
walk(root, 1, &depths)
|
|
|
|
return root
|
|
}
|
|
|
|
var _ = connect
|