Minimum Edge Toggles on a Tree Problem
Description
LeetCode Problem 3812.
You are given an undirected tree with n nodes, numbered from 0 to n - 1. It is represented by a 2D integer array edges of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.
You are also given two binary strings start and target of length n. For each node x, start[x] is its initial color and target[x] is its desired color.
In one operation, you may pick an edge with index i and toggle both of its endpoints. That is, if the edge is [u, v], then the colors of nodes u and v each flip from '0' to '1' or from '1' to '0'.
Return an array of edge indices whose operations transform start into target. Among all valid sequences with minimum possible length, return the edge indices in increasing order.
If it is impossible to transform start into target, return an array containing a single element equal to -1.
Example 1:

Input: n = 3, edges = [[0,1],[1,2]], start = "010", target = "100"
Output: [0]
Explanation:
Toggle edge with index 0, which flips nodes 0 and 1.
The string changes from "010" to "100", matching the target.
Example 2:

Input: n = 7, edges = [[0,1],[1,2],[2,3],[3,4],[3,5],[1,6]], start = "0011000", target = "0010001"
Output: [1,2,5]
Explanation:
- Toggle edge with index 1, which flips nodes 1 and 2.
- Toggle edge with index 2, which flips nodes 2 and 3.
- Toggle edge with index 5, which flips nodes 1 and 6.
After these operations, the resulting string becomes "0010001", which matches the target.
Example 3:

Input: n = 2, edges = [[0,1]], start = "00", target = "01"
Output: [-1]
Explanation:
There is no sequence of edge toggles that transforms "00" into "01". Therefore, we return [-1].
Constraints:
2 <= n == start.length == target.length <= 10^5edges.length == n - 1edges[i] = [a_i, b_i]0 <= a_i, b_i < nstart[i]is either'0'or'1'.target[i]is either'0'or'1'.- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: DFS
Each edge is used at most once, and in a tree the set of edges to toggle is forced: the edge to a node's parent must be toggled exactly when the node still has the wrong color after its child edges are handled.
Run dfs(a, parent), which returns whether node a still needs a flip through the edge to its parent. Start with rev = start[a] != target[a]. For each child b reached by edge i, if dfs(b, a) is true, record edge i and flip rev. Return rev.
If the root still needs a flip, the target cannot be reached, so return [-1]. Otherwise return the recorded edges in increasing order.
Complexity: Time O(n log n), space O(n), where n is the number of nodes.
class Solution:
def minimumFlips(
self, n: int, edges: List[List[int]], start: str, target: str
) -> List[int]:
g = [[] for _ in range(n)]
for i, (a, b) in enumerate(edges):
g[a].append((b, i))
g[b].append((a, i))
ans = []
def dfs(a: int, fa: int) -> bool:
rev = start[a] != target[a]
for b, i in g[a]:
if b != fa and dfs(b, a):
ans.append(i)
rev = not rev
return rev
if dfs(0, -1):
return [-1]
ans.sort()
return ans