Skip to content

Commit

Permalink
Merge b95bcba into e3d5c7c
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Dec 8, 2018
2 parents e3d5c7c + b95bcba commit 3154db6
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ problems from
* [Day 104](https://github.com/vaskoz/dailycodingproblem-go/issues/219)
* [Day 105](https://github.com/vaskoz/dailycodingproblem-go/issues/221)
* [Day 106](https://github.com/vaskoz/dailycodingproblem-go/issues/223)
* [Day 107](https://github.com/vaskoz/dailycodingproblem-go/issues/225)
67 changes: 67 additions & 0 deletions day107/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package day107

// BinaryTree is a binary tree of integers.
type BinaryTree struct {
Value int
Left, Right *BinaryTree
}

// BinaryTreeByLevel prints the binary tree level-wise.
// Runs in O(N^2) time.
func BinaryTreeByLevel(tree *BinaryTree) []int {
var result []int
for i := 0; i < Height(tree); i++ {
result = append(result, GetLevel(tree, i)...)
}
return result
}

// GetLevel returns all the nodes at the same height from left to right.
func GetLevel(tree *BinaryTree, level int) []int {
if tree == nil {
return nil
}
if level == 0 {
return []int{tree.Value}
}
var result []int
result = append(result, GetLevel(tree.Left, level-1)...)
result = append(result, GetLevel(tree.Right, level-1)...)
return result
}

// Height returns the height of a binary tree.
func Height(tree *BinaryTree) int {
if tree == nil {
return 0
}
return 1 + max(Height(tree.Left), Height(tree.Right))
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

// BinaryTreeByLevelFaster prints the binary tree level-wise faster than O(N^2).
// This version runs in O(N) with O(N) extra space.
func BinaryTreeByLevelFaster(tree *BinaryTree) []int {
var result []int
level := []*BinaryTree{tree}
for len(level) != 0 {
nextLevel := make([]*BinaryTree, 0, 2*len(level))
for _, node := range level {
result = append(result, node.Value)
if node.Left != nil {
nextLevel = append(nextLevel, node.Left)
}
if node.Right != nil {
nextLevel = append(nextLevel, node.Right)
}
}
level = nextLevel
}
return result
}
54 changes: 54 additions & 0 deletions day107/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package day107

import (
"reflect"
"testing"
)

var testcases = []struct {
tree *BinaryTree
expected []int
}{
{&BinaryTree{1,
&BinaryTree{2, nil, nil},
&BinaryTree{3, &BinaryTree{4, nil, nil}, &BinaryTree{5, nil, nil}}},
[]int{1, 2, 3, 4, 5}},
{&BinaryTree{1,
&BinaryTree{3, &BinaryTree{4, nil, nil}, &BinaryTree{5, nil, nil}},
&BinaryTree{2, nil, nil}},
[]int{1, 3, 2, 4, 5}},
}

func TestBinaryTreeByLevel(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := BinaryTreeByLevel(tc.tree); !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected %v got %v", tc.expected, result)
}
}
}

func BenchmarkBinaryTreeByLevel(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
BinaryTreeByLevel(tc.tree)
}
}
}

func TestBinaryTreeByLevelFaster(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := BinaryTreeByLevelFaster(tc.tree); !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected %v got %v", tc.expected, result)
}
}
}

func BenchmarkBinaryTreeByLevelFaster(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
BinaryTreeByLevelFaster(tc.tree)
}
}
}

0 comments on commit 3154db6

Please sign in to comment.