There is an undirected tree with n nodes labeled from 1 to n, rooted at node 1. The tree is represented by a 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi.
Initially, all edges have a weight of 0. You must assign each edge a weight of either 1 or 2.
The cost of a path between any two nodes u and v is the total weight of all edges in the path connecting them.
Select any one node x at the maximum depth. Return the number of ways to assign edge weights in the path from node 1 to x such that its total cost is odd.
Since the answer may be large, return it modulo 109 + 7.
Note: Ignore all edges not in the path from node 1 to x.
Example 1:

Input: edges = [[1,2]]
Output: 1
Explanation:
1 → 2).Example 2:

Input: edges = [[1,2],[1,3],[3,4],[3,5]]
Output: 2
Explanation:
1 → 3 and 3 → 4).
Constraints:
2 <= n <= 105edges.length == n - 1edges[i] == [ui, vi]1 <= ui, vi <= nedges represents a valid tree.Problem Overview: You are given a tree where edge weights are not fixed. The task is to count how many valid ways you can assign weights to the edges while satisfying constraints on distances along tree paths. Because the structure is a tree, every pair of nodes has exactly one simple path, which allows the constraints to be analyzed using a single traversal.
Approach 1: Brute Force Enumeration (Exponential Time)
The most direct idea is to try every possible weight assignment for every edge and verify whether the resulting tree satisfies the required distance conditions. If each edge can take two possible weights, the total number of assignments is 2^(n-1) for a tree with n nodes. For each assignment, compute path distances and validate the constraints. This approach runs in O(2^n * n) time with O(n) space. It only works for very small trees and mainly helps build intuition about which edges actually influence the constraints.
Approach 2: Tree DFS + Combinatorics (O(n))
The key observation is that constraints on distances only affect edges that lie on specific paths in the tree. Because a tree has a unique path between any two nodes, you can analyze the structure using a single Depth-First Search. During DFS, track how many constrained nodes appear in each subtree. When a subtree contributes to a constrained path, the connecting edge becomes restricted; otherwise it remains free to choose from the allowed weight options.
Each unrestricted edge multiplies the number of valid assignments. If an edge has two valid weight choices, it contributes a factor of 2 to the total count. By counting how many edges remain unrestricted during the DFS traversal, the final result becomes a simple power calculation such as 2^k where k is the number of flexible edges. The traversal processes every node and edge once, giving O(n) time complexity and O(n) space for recursion and adjacency storage.
This method works because trees eliminate cycles, so each edge’s contribution can be determined independently once the subtree structure is known. DFS naturally exposes parent–child relationships and subtree counts, which makes it ideal for these kinds of constraint propagation problems in tree structures combined with mathematical counting.
Recommended for interviews: The DFS + combinatorics approach is the expected solution. Interviewers typically want to see that you recognize the unique-path property of trees, propagate constraints with a single traversal, and convert the remaining freedom into a mathematical counting formula. Mentioning the brute-force enumeration first shows you understand the search space, but deriving the O(n) DFS counting solution demonstrates stronger algorithmic reasoning.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Weight Enumeration | O(2^n * n) | O(n) | Only for very small trees or for understanding the constraint behavior |
| DFS + Combinatorial Counting | O(n) | O(n) | Optimal approach for large trees where constraints affect only specific paths |
3558. Number of Ways to Assign Edge Weights I| BiWeekly 157 | 3rd Problem • Tech Courses • 272 views views
Watch 1 more video solutions →Practice Number of Ways to Assign Edge Weights I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor