I have been working on nailing down the Binary Search Tree over the past couple days and about a day ago a coding challenge landed in my inbox that used a binary tree.
Below is the link to the Go Playground with the code. I would love feedback on what I could do to make it better (I know better is subjective…)
The challenge read:
Two nodes in a binary tree can be called cousins if they are on the same level of the tree but have different parents. For example, in the following diagram 4 and 6 are cousins.
1 / \ 2 3 / \ \ 4 5 6
Given a binary tree and a particular node, find all cousins of that node.
Link to the code:
The tree in my code is visualized as:
10 / \ 5 15 / \ / \ 2 6 12 22 / \ \ \ 1 4 7 25