682. Find Nodes at Distance K in Binary Tree
0
Easy
Given the root of a binary tree, a target node value, and an integer k, return an array of node values that are at a distance of k from the target node.
Input Format
The binary tree is represented as a string input. Each node is represented as "root left_child right_child" of the current node, separated by spaces. The character 'N' represents a null node. The second line of the input contains the target node value and the distance k.
Output Format
Print the values of all nodes that are at a distance of k from the target node.
Example
Input
3 5 1 6 2 0 8 N N 7 4
5 2
Output
7 4 1
Constraints
The number of nodes in the binary tree ranges from 1 to 500. Each node has a value between 0 and 500, and all node values are unique. The target node value is one of the values in the tree. The distance k is a non-negative integer between 0 and 1000.
Loading...
View Submissions
Console