Skip to content

Practice Cousins in Binary Tree II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2641
LeetCode Problem

Cousins in Binary Tree II

Problem Statement

Given the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values. Two nodes of a binary tree are cousins if they have the same depth with different parents. Return the root of the modified tree. Note that the depth of a node is the number of edges in the path from the root node to it.

Source: Cousins in Binary Tree II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [5,4,9,1,10,null,7]
Output
[0,0,0,7,7,null,11]

# Example 2

Input
root = [3,1,2]
Output
[0,0,0]

Constraints

The number of nodes in the tree is in the range [1, 10^5].
1 <= Node.val <= 10^4

Related LeetCode Problems

Practice more problems similar to Cousins in Binary Tree II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.