Skip to content

Instantly share code, notes, and snippets.

@wardenlym
Last active February 26, 2019 15:22
Show Gist options
  • Save wardenlym/a07438981e02051824fed7428c12c99e to your computer and use it in GitHub Desktop.
Save wardenlym/a07438981e02051824fed7428c12c99e to your computer and use it in GitHub Desktop.
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