add new solutions

This commit is contained in:
kanna5 2026-01-05 16:48:03 +09:00
parent 0c73608ce5
commit d798d5e8c9
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
19 changed files with 661 additions and 4 deletions

View file

@ -0,0 +1,34 @@
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