Last active
February 26, 2019 15:22
-
-
Save wardenlym/a07438981e02051824fed7428c12c99e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TreeNode struct { | |
Val int | |
Left *TreeNode | |
Right *TreeNode | |
} | |
func order1(root *TreeNode) [][]int { | |
r := [][]int{} | |
collect := func(nodes []*TreeNode) ([]int, []*TreeNode) { | |
level := []int{} | |
next := []*TreeNode{} | |
for _, n := range nodes { | |
if n != nil { | |
level = append(level, n.Val) | |
if n.Left != nil { | |
next = append(next, n.Left) | |
} | |
if n.Right != nil { | |
next = append(next, n.Right) | |
} | |
} | |
} | |
return level, next | |
} | |
func() { | |
level := []int{} | |
next := []*TreeNode{root} | |
for { | |
level, next = collect(next) | |
if len(level) == 0 { | |
return | |
} | |
r = append(r, level) | |
} | |
}() | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment