add new solutions
This commit is contained in:
parent
9c2c959a9b
commit
9a10695e8c
29 changed files with 1074 additions and 2 deletions
35
solutions/1/q114/solution.go
Normal file
35
solutions/1/q114/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q114
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func fl(node *TreeNode) (head, tail *TreeNode) {
|
||||
if node == nil {
|
||||
return nil, nil
|
||||
}
|
||||
head, tail = node, node
|
||||
|
||||
lhead, ltail := fl(node.Left)
|
||||
rhead, rtail := fl(node.Right)
|
||||
|
||||
if lhead != nil {
|
||||
node.Left = nil
|
||||
node.Right = lhead
|
||||
tail = ltail
|
||||
}
|
||||
|
||||
if rhead != nil {
|
||||
tail.Right = rhead
|
||||
tail = rtail
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func flatten(root *TreeNode) {
|
||||
fl(root)
|
||||
}
|
||||
|
||||
var _ = flatten
|
||||
Loading…
Add table
Add a link
Reference in a new issue