add new solutions
This commit is contained in:
parent
0c73608ce5
commit
d798d5e8c9
19 changed files with 661 additions and 4 deletions
34
solutions/1/q117/solution.go
Normal file
34
solutions/1/q117/solution.go
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue