Sum of Products in Category Tree
At Nykaa, our extensive product catalog is often organized hierarchically into categories and subcategories. To understand the total inventory footprint, it's crucial to aggregate product counts across these structures. This problem simulates a simplified version of our category management system, represented as a binary tree.
You are given the root of a binary tree. Each node in this tree has an integer value, node.val, representing the number of unique products directly associated with that specific category. Your task is to calculate the total number of unique products across all categories represented in the entire tree.
Input:
root: The root node of a binary tree. Each node.val is a non-negative integer.Output:
node.val values in the tree.root is null, it means there are no categories, and thus no products. In this case, the total sum should be 0.node.val) will always be non-negative.Examples
Example 1
Input:
root = [10,5,15,null,null,12,20]
Output:
62
Note:
The sum of all node values is 10 (root) + 5 (left child) + 15 (right child) + 12 (right-left grandchild) + 20 (right-right grandchild) = 62.
Example 2
Input:
root = [1]
Output:
1
Note:
The tree has only one node with value 1, so the total sum is 1.
Example 3
Input:
root = []
Output:
0
Note:
The tree is empty (root is null), so the total sum of products is 0.
Constraints
0 and 10^4.0 <= Node.val <= 1000.left and right pointers for child nodes.10^4 nodes).Loading editor...