Skip to content

Commit

Permalink
day 258: simplify the logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed May 9, 2019
1 parent 66cc8d4 commit 01f3a21
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions day258/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,23 @@ func BoustrophedonOrder(root *IntBinaryTree) []int {
leftToRight := true
for len(levels) != 0 {
var nextLevel []*IntBinaryTree
if leftToRight {
for _, n := range levels {
result = append(result, n.Value)
if n.Left != nil {
nextLevel = append(nextLevel, n.Left)
}
if n.Right != nil {
nextLevel = append(nextLevel, n.Right)
}
for _, n := range levels {
result = append(result, n.Value)
if n.Left != nil {
nextLevel = append(nextLevel, n.Left)
}
} else {
for i := len(levels) - 1; i >= 0; i-- {
result = append(result, levels[i].Value)
j := len(levels) - 1 - i
if levels[j].Left != nil {
nextLevel = append(nextLevel, levels[j].Left)
}
if levels[j].Right != nil {
nextLevel = append(nextLevel, levels[j].Right)
}
if n.Right != nil {
nextLevel = append(nextLevel, n.Right)
}
}
if !leftToRight {
toReverse := result[len(result)-len(levels):]
for i := 0; i < len(toReverse)/2; i++ {
toReverse[i], toReverse[len(toReverse)-1-i] = toReverse[len(toReverse)-1-i], toReverse[i]
}
}
levels = nextLevel
leftToRight = !leftToRight
levels = nextLevel
}
return result
}

0 comments on commit 01f3a21

Please sign in to comment.