#K71482. Sum of Nodes at Distance K in Binary Tree
Sum of Nodes at Distance K in Binary Tree
Sum of Nodes at Distance K in Binary Tree
You are given a binary tree and an integer \(K\). Your task is to compute the sum of the node values that are exactly at distance \(K\) from the root. The distance is defined as the number of edges in the unique path from the root to the node. For example, when \(K=2\), you must sum the values of all nodes that are two edges away from the root.
Note: The binary tree is provided in level order format where missing nodes are represented by the string "null".
Input Format: In the first line, a space-separated list represents the level order traversal of the binary tree. In the second line, an integer \(K\) is provided.
Output Format: Output a single integer representing the sum of the node values at distance \(K\) from the root.
inputFormat
The input is given in two lines. The first line contains a space-separated list of node values representing the binary tree in level order. Use "null" (without quotes) to denote missing nodes. The second line contains a single integer \(K\), which represents the target distance from the root.
For example:
1 2 3 4 5 6 7 8 null null null null null null 2
outputFormat
Output a single integer, which is the sum of the node values at distance \(K\) from the root of the given binary tree.
For the input above, the output is:
22## sample
1 2 3 4 5 6 7 8 null null null null null null
2
22