Skip to content

Commit

Permalink
zigxag traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoanh An committed Dec 29, 2019
1 parent ff748f9 commit f6225fd
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 99+ Coding Interview Problems with Solutions
# 100+ Coding Interview Problems with Solutions

[![Go Report Card](https://goreportcard.com/badge/github.com/hoanhan101/algo)
](https://goreportcard.com/report/github.com/hoanhan101/algo)
Expand Down Expand Up @@ -187,6 +187,7 @@ that you can prepare better for your next coding interviews.
- [Reverse a linked list](gtci/reverse_list_test.go)
- Tree breath first search
- [Binary tree level order traversal](gtci/level_order_traversal_test.go)
- [Zigzag traversal](gtci/zigzag_traversal_test.go)
- **Other**
- Data structures
- Linked List
Expand Down
115 changes: 115 additions & 0 deletions gtci/zigzag_traversal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Problem:
- Given a binary tree, populate the values of all nodes of each level
in a zigzag order in separate sub-arrays.
Example:
- Input:
1
2 3
4 5
Output: [][]interface{}{[]interface{}{1}, []interface{}{3, 2}, []interface{}{4, 5}}
Approach:
- Start by pushing the root node to a queue.
- Keep iterating until the queue is empty.
- At each step,
- use a linked list to push front or back depending on the zigzag direction
- enqueue its left and right child
Cost:
- O(n) time, O(n) space.
*/

package gtci

import (
"testing"

"github.com/hoanhan101/algo/common"
)

func TestZigzagTraverse(t *testing.T) {
t1 := &common.TreeNode{}

t2 := &common.TreeNode{nil, 1, nil}

t3 := &common.TreeNode{nil, 1, nil}
t3.Left = &common.TreeNode{nil, 2, nil}
t3.Left.Right = &common.TreeNode{nil, 3, nil}

t4 := &common.TreeNode{nil, 1, nil}
t4.Left = &common.TreeNode{nil, 2, nil}
t4.Right = &common.TreeNode{nil, 3, nil}

t5 := &common.TreeNode{nil, 1, nil}
t5.Left = &common.TreeNode{nil, 2, nil}
t5.Right = &common.TreeNode{nil, 3, nil}
t5.Left.Left = &common.TreeNode{nil, 4, nil}
t5.Right.Right = &common.TreeNode{nil, 5, nil}

tests := []struct {
in *common.TreeNode
expected [][]interface{}
}{
{t1, [][]interface{}{[]interface{}{0}}},
{t2, [][]interface{}{[]interface{}{1}}},
{t3, [][]interface{}{[]interface{}{1}, []interface{}{2}, []interface{}{3}}},
{t4, [][]interface{}{[]interface{}{1}, []interface{}{3, 2}}},
{t5, [][]interface{}{[]interface{}{1}, []interface{}{3, 2}, []interface{}{4, 5}}},
}

for _, tt := range tests {
common.Equal(
t,
tt.expected,
zigzagTraverse(tt.in),
)
}
}

func zigzagTraverse(root *common.TreeNode) [][]interface{} {
out := [][]interface{}{}

if root == nil {
return out
}

// initialize a linked list with the root.
queue := common.NewQueue()
queue.Push(root)
leftToRight := true

for queue.Size() > 0 {
levelSize := queue.Size()
currentLevel := common.NewList()

for i := 0; i < levelSize; i++ {
// pop the queue and cache that value to its current level.
current := queue.Pop().(*common.TreeNode)

if leftToRight {
currentLevel.PushBack(current.Value)
} else {
currentLevel.PushFront(current.Value)
}

// push its left child.
if current.Left != nil {
queue.Push(current.Left)
}

// push its right child.
if current.Right != nil {
queue.Push(current.Right)
}
}

out = append(out, currentLevel.Slice())

// reverse its direction.
leftToRight = !leftToRight
}

return out
}

0 comments on commit f6225fd

Please sign in to comment.