A relay operator manages a hierarchical broadcast network of n towers arranged as a rooted binary tree: tower 0 is the network's hub, and every other tower is reached from the hub by following a chain of left/right relay links. Each tower continuously transmits on one integer frequency. The operator wants to know whether the entire network is broadcasting on a single uniform frequency, i.e. whether every tower's frequency equals the hub's frequency.
n — the number of towers (1 <= n <= 100).n lines describes tower i (0-indexed, i from 0 to n-1) as three integers val_i left_i right_i, where val_i (0 <= val_i <= 99) is tower i's frequency, and left_i and right_i are the indices of tower i's left and right child towers, or -1 if that side has no child. Tower 0 is the hub, and the described structure always forms a valid rooted binary tree containing all n towers.Print true if every tower's frequency equals the hub's frequency, or false otherwise.
1 <= n <= 1000 <= val_i <= 99left_i or right_i is a valid tower index in [0, n-1], each tower is the child of at most one other tower, and the whole structure is a valid rooted binary tree rooted at tower 0 (every tower is reachable from tower 0).Example 1
Input
4 5 1 2 5 3 -1 5 -1 -1 5 -1 -1
Expected
true
Explanation
All 4 towers (0 through 3) transmit on frequency 5, the same as the hub's frequency, so the whole network is uniform: true.
Example 2
Input
3 5 1 2 5 -1 -1 7 -1 -1
Expected
false
Explanation
Tower 1 transmits on frequency 5 like the hub, but tower 2 (the hub's other child) transmits on frequency 7, which differs from the hub's frequency of 5. The network is not uniform: false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →