Search
⌘K

Leetcode 2641. Cousins in Binary Tree II

Replace each node's value with the sum of all nodes at the same depth that have different parents (its cousins) and return the modified tree. This is typically solved by level-order traversal computing level sums and excluding sibling groups to get each node's cousin sum in O(n) time and O(width) space (n up to 1e5).


Question Timeline

See when this question was last asked and where, including any notes left by other candidates.

Comments

Your account is free and you can post anonymously if you choose.