![]() |
VOOZH | about |
Given a binary tree and a source node "start" which transmits a network signal. The signal propagates to neighboring nodes every second, the task is to determine the minimum amount of time required for the entire tree to receive the signal.
Examples:
Input:
11
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Start = 14
Output: 3Input:
4
/ \
3 1
\
2
Start = 1
Output: 2
Start from the given node "start" and use a Breadth-First Search (BFS) approach. Propagate signal to neighboring nodes during the BFS process and keep track of time until all nodes have been visited. To handle the issue of determining a node's parent, create a data structure that maintains parent-node relationships, this will allowing to track each node's parent.
Step-by-step approach:
Below is the implementation of the above approach:
3
Time Complexity: O(N), where N is the number of nodes in the binary tree.
Auxiliary Space: O(N)