Skip to content

Commit

Permalink
Merge pull request #170 from vaskoz/day80
Browse files Browse the repository at this point in the history
Day80
  • Loading branch information
vaskoz committed Nov 9, 2018
2 parents c0aae32 + 3fc5670 commit 432fb61
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ problems from
* [Day 10](https://github.com/vaskoz/dailycodingproblem-go/issues/21)
* [Day 11](https://github.com/vaskoz/dailycodingproblem-go/issues/23)
* [Day 12](https://github.com/vaskoz/dailycodingproblem-go/issues/25)
* [Day 12 Count and Deepest](https://github.com/vaskoz/dailycodingproblem-go/issues/27)
* [Day 12 Count](https://github.com/vaskoz/dailycodingproblem-go/issues/27)
* [Day 13](https://github.com/vaskoz/dailycodingproblem-go/issues/29)
* [Day 14](https://github.com/vaskoz/dailycodingproblem-go/issues/31)
* [Day 15](https://github.com/vaskoz/dailycodingproblem-go/issues/33)
Expand Down Expand Up @@ -89,3 +89,4 @@ problems from
* [Day 77](https://github.com/vaskoz/dailycodingproblem-go/issues/163)
* [Day 78](https://github.com/vaskoz/dailycodingproblem-go/issues/165)
* [Day 79](https://github.com/vaskoz/dailycodingproblem-go/issues/167)
* [Day 80](https://github.com/vaskoz/dailycodingproblem-go/issues/168)
15 changes: 15 additions & 0 deletions countBinaryTreeNodes/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package deepest

// BinaryTree represents a tree with a left and right subtree.
type BinaryTree struct {
// val int // value doesn't matter here.
left, right *BinaryTree
}

// CountNodes tallies the total number of nodes in the binary tree.
func CountNodes(root *BinaryTree) int {
if root == nil {
return 0
}
return CountNodes(root.left) + CountNodes(root.right) + 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,3 @@ func BenchmarkCountNodes(b *testing.B) {
CountNodes(root)
}
}

func TestDeepest(t *testing.T) {
t.Parallel()
root := buildTestTree()
if _, count := Deepest(root); count != 4 {
t.Errorf("Expected 4 but got %v", count)
}
}

func BenchmarkDeepest(b *testing.B) {
root := buildTestTree()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Deepest(root)
}
}
10 changes: 1 addition & 9 deletions deepestBinaryTree/problem.go → day80/problem.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
package deepest
package day80

// BinaryTree represents a tree with a left and right subtree.
type BinaryTree struct {
// val int // value doesn't matter here.
left, right *BinaryTree
}

// CountNodes tallies the total number of nodes in the binary tree.
func CountNodes(root *BinaryTree) int {
if root == nil {
return 0
}
return CountNodes(root.left) + CountNodes(root.right) + 1
}

// Deepest returns the deepest node in the binary tree.
func Deepest(root *BinaryTree) (*BinaryTree, int) {
if root != nil && root.left == nil && root.right == nil {
Expand Down
26 changes: 26 additions & 0 deletions day80/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package day80

import "testing"

// nodes = 5 depth = 3
func buildTestTree() *BinaryTree {
return &BinaryTree{&BinaryTree{&BinaryTree{nil, nil},
&BinaryTree{&BinaryTree{nil, nil}, nil}},
&BinaryTree{nil, &BinaryTree{nil, nil}}}
}

func TestDeepest(t *testing.T) {
t.Parallel()
root := buildTestTree()
if _, count := Deepest(root); count != 4 {
t.Errorf("Expected 4 but got %v", count)
}
}

func BenchmarkDeepest(b *testing.B) {
root := buildTestTree()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Deepest(root)
}
}

0 comments on commit 432fb61

Please sign in to comment.