-
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.
- Loading branch information
Showing
3 changed files
with
129 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,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) | ||
} |
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,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) | ||
} | ||
} | ||
} |