You are given a tree with n nodes labeled from 0 to n - 1. The tree represents an organizational hierarchy.
A node is a key team if it is an endpoint of at least one longest path in the tree. A longest path in a tree is also called a tree diameter.
Return a binary array answer of length n, where answer[i] = 1 if node i is a key team, and 0 otherwise.
Examples
01 · Example 1
n = 4 edges = [[0, 1], [1, 2], [2, 3]] return = [1, 0, 0, 1]
The only diameter is the path from node 0 to node 3, so those two nodes are marked.
02 · Example 2
n = 4 edges = [[0, 1], [0, 2], [0, 3]] return = [0, 1, 1, 1]
Every longest path goes between two leaves, so nodes 1, 2, and 3 are all endpoints of some diameter.
Constraints
1 <= nedges.length == n - 1- The edges form a tree.
More Salesforce problems
- System Energy ReductionOA · Seen Mar 2026
- Update Logs by Symmetric XOROA · Seen Mar 2026
- Count Palindromic Concatenation PairsOA · Seen Mar 2026
- Collect Opportunity Data in a TreeOA · Seen Feb 2026
- Replace '?' to Avoid Adjacent DuplicatesOA · Seen Feb 2026
- Strings With No k Consecutive Identical CharactersOA · Seen Feb 2026
- Spam ClassificationSeen Jun 2025
- Longest Subsequence which is a SubstringSeen Jun 2025
public int[] keyTeamsInTree(int n, int[][] edges) {
// write your code here
}n4
edges[[0, 1], [1, 2], [2, 3]]
expected[1, 0, 0, 1]
sign in to submit