Skip to content

Commit

Permalink
Merge 6a67d70 into 28c6b46
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Jun 2, 2019
2 parents 28c6b46 + 6a67d70 commit c49f7e6
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,4 @@ problems from
* [Day 281](https://github.com/vaskoz/dailycodingproblem-go/issues/572)
* [Day 282](https://github.com/vaskoz/dailycodingproblem-go/issues/575)
* [Day 283](https://github.com/vaskoz/dailycodingproblem-go/issues/577)
* [Day 284](https://github.com/vaskoz/dailycodingproblem-go/issues/578)
46 changes: 46 additions & 0 deletions day284/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package day284

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

// FindCousins returns all the cousins of a given node.
func FindCousins(root *BinaryTree, node int) []int {
nodeLevel := level(root, node, 1)
return levelCousins(root, node, nodeLevel, []int{})
}

func levelCousins(root *BinaryTree, node, lev int, cousins []int) []int {
if root == nil || lev < 2 {
return cousins
}
if lev == 2 {
if (root.Left != nil && root.Left.Value == node) ||
(root.Right != nil && root.Right.Value == node) {
return cousins
}
if root.Left != nil {
cousins = append(cousins, root.Left.Value)
}
if root.Right != nil {
cousins = append(cousins, root.Right.Value)
}
} else if lev > 2 {
cousins = levelCousins(root.Left, node, lev-1, cousins)
cousins = levelCousins(root.Right, node, lev-1, cousins)
}
return cousins
}

func level(root *BinaryTree, node, lev int) int {
if root == nil {
return 0
} else if root.Value == node {
return lev
} else if down := level(root.Left, node, lev+1); down != 0 {
return down
}
return level(root.Right, node, lev+1)
}
82 changes: 82 additions & 0 deletions day284/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package day284

import (
"reflect"
"testing"
)

// nolint
var testcases = []struct {
root *BinaryTree
node int
cousins []int
}{
{
&BinaryTree{
1,
&BinaryTree{
2,
&BinaryTree{4, nil, nil},
&BinaryTree{5, nil, nil},
},
&BinaryTree{
3,
nil,
&BinaryTree{6, nil, nil},
},
},
4,
[]int{6},
},
{
&BinaryTree{
1,
&BinaryTree{
2,
&BinaryTree{4, nil, nil},
&BinaryTree{5, nil, nil},
},
&BinaryTree{
3,
nil,
&BinaryTree{6, nil, nil},
},
},
10,
[]int{},
},
{
&BinaryTree{
1,
&BinaryTree{
2,
&BinaryTree{4, nil, nil},
&BinaryTree{5, nil, nil},
},
&BinaryTree{
3,
nil,
&BinaryTree{6, nil, nil},
},
},
6,
[]int{4, 5},
},
}

func TestFindCousins(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if cousins := FindCousins(tc.root, tc.node); !reflect.DeepEqual(cousins, tc.cousins) {
t.Errorf("Expected %v, got %v", tc.cousins, cousins)
}
}
}

func BenchmarkFindCousins(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
FindCousins(tc.root, tc.node)
}
}
}

0 comments on commit c49f7e6

Please sign in to comment.