-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from vaskoz/day80
Day80
- Loading branch information
Showing
5 changed files
with
44 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |