Question
Positive Nodes Summation
You are given the root of a binary tree consisting of N nodes. Each node contains a non-zero integer value, which may be positive or negative.
Your task is to calculate and return the sum of all positive-valued nodes present in the tree.
Input
User Task
Since this is a functional problem, you do not need to handle input or output. Your task is to complete the function pos_node_sum(), that takes the root node as an input parameter.
Custom Input
The first line of input contains a positive integer N, representing the number of nodes in the tree.
The next line consists of space-separated integers denoting the level order traversal of the tree, where non-zero values represent node values, and 0 indicates a null node.
Note: Once a node is marked as 0, no further information about its children is provided.
Since this is a functional problem, you do not need to handle input or output. Your task is to complete the function pos_node_sum(), that takes the root node as an input parameter.
Custom Input
The first line of input contains a positive integer N, representing the number of nodes in the tree.
The next line consists of space-separated integers denoting the level order traversal of the tree, where non-zero values represent node values, and 0 indicates a null node.
Note: Once a node is marked as 0, no further information about its children is provided.
Output
Return an integer representing the sum of all positive node values present in the tree.
Example
Input
13
1 2 3 4 5 -1 -2 -3 -4 -5 6 0 0 0 0 0 0 0 0 0 0 -6 -7
Output
21
Explanation
The below given tree represents the binary tree described in the input:
The sum of positive node values (marked in white) is = 1 + 2 + 3 + 4 + 5 + 6 = 21
13
1 2 3 4 5 -1 -2 -3 -4 -5 6 0 0 0 0 0 0 0 0 0 0 -6 -7
Output
21
Explanation
The below given tree represents the binary tree described in the input:
The sum of positive node values (marked in white) is = 1 + 2 + 3 + 4 + 5 + 6 = 21