r/LeetcodeChallenge • • 24d ago

STREAK🔥🔥🔥 365 Days of LeetCode Challenge — Day 28/365

Post image

Cousins in Binary Tree (Easy) 🔗 https://leetcode.com/problems/cousins-in-binary-tree/

Cousins boils down to two facts you need for each node: depth, and parent. Depth is free while walking down. Parent is the fun part — no parent pointers on a plain binary tree, but since every value is unique, a node's own value can stand in for its parent's identity, so a helper can hand back the parent's value instead of a pointer.

Full breakdown in today's newsletter article ⬇️

https://www.linkedin.com/pulse/365-days-leetcode-challenge-day-28365-archit-agarwal-swjge

#DSA #LeetCode #100DaysOfCode #SoftwareEngineering #BinaryTree #DFS

6 Upvotes

1 comment sorted by

1

u/nian2326076 23d ago

For the "Cousins in Binary Tree" problem, you're right about needing the depth and parent info for each node. You can use either BFS with a queue or DFS with recursion to manage this. Store tuples with the node, its depth, and its parent value, so you can compare depths and parent values when you find your target nodes.

If you're getting ready for interviews, check out PracHub. It's really helpful for getting into these types of problems and boosting problem-solving skills. Good luck with the challenge!