So for today I decided to go back to a problem I previously skipped because it hurt my brain to think about. I’ve never exactly
been an authority on binary search trees, much less just binary trees, so I knew this was going to hurt.
It turns out (as usual) there’s a couple different ways you can go about this. I really wanted to work on my breadth-first search
algorithm skills and I feel like that’s the intuitive solution here, so that’s what I did.
At each level of the binary tree, I add all the children to a queue for processing while checking to see if any of the nodes are my target nodes.
If I manage to find two non-nil nodes at the same depth in the tree, I can quickly check to see if their parents are the same, and if they’re not,
I’ve found my cousins.
I can also short-circuit the entire thing if I find one target node but not the other at any given depth and just return false, which I’ve added to the algorithm.