-
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 #234 from vaskoz/day110
Day110
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 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,32 @@ | ||
package day110 | ||
|
||
// IntBinaryTree is a binary tree with values that are integers. | ||
type IntBinaryTree struct { | ||
Value int | ||
Left, Right *IntBinaryTree | ||
} | ||
|
||
// AllPathsToLeaves return the nodes encountered from the root | ||
// to each leave. | ||
func AllPathsToLeaves(tree *IntBinaryTree) [][]int { | ||
return pathHolder(tree, []int{}) | ||
} | ||
|
||
func pathHolder(tree *IntBinaryTree, path []int) [][]int { | ||
path = append(path, tree.Value) | ||
if tree.Left == nil && tree.Right == nil { | ||
leavePath := make([]int, len(path)) | ||
copy(leavePath, path) | ||
return [][]int{leavePath} | ||
} | ||
var result [][]int | ||
if tree.Left != nil { | ||
leftSubtree := pathHolder(tree.Left, path) | ||
result = append(result, leftSubtree...) | ||
} | ||
if tree.Right != nil { | ||
rightSubtree := pathHolder(tree.Right, path) | ||
result = append(result, rightSubtree...) | ||
} | ||
return result | ||
} |
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,35 @@ | ||
package day110 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testcases = []struct { | ||
head *IntBinaryTree | ||
expected [][]int | ||
}{ | ||
{&IntBinaryTree{Value: 1, | ||
Left: &IntBinaryTree{2, nil, nil}, | ||
Right: &IntBinaryTree{3, | ||
&IntBinaryTree{4, nil, nil}, | ||
&IntBinaryTree{5, nil, nil}}}, | ||
[][]int{{1, 2}, {1, 3, 4}, {1, 3, 5}}}, | ||
} | ||
|
||
func TestAllPathsToLeaves(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := AllPathsToLeaves(tc.head); !reflect.DeepEqual(tc.expected, result) { | ||
t.Errorf("Expected %v got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkAllPathsToLeaves(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
AllPathsToLeaves(tc.head) | ||
} | ||
} | ||
} |