P20915 [GeeksforGeeks] 0f02fb492c69c094d94e806cba6ff8d8cd04d630
普及
题目描述

Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20

输入格式

见题面

输出格式

见题面

样例输入
1 / \ 2 9 / / \ 4 5 7 / \ / \ 8 19 20 11 / / \ 30 40 50 target = 9, K = 1
样例输出
22