Minimum Flips in Binary Tree to Get Result - Problem

Imagine you have a binary expression tree where each leaf contains a boolean value (0 for false, 1 for true) and each internal node represents a boolean operation:

  • 2 = OR operation
  • 3 = AND operation
  • 4 = XOR operation
  • 5 = NOT operation (has only one child)

Your goal is to find the minimum number of leaf flips needed to make the entire tree evaluate to a desired result. A flip changes 0โ†’1 or 1โ†’0.

Key Challenge: You need to work backwards from the root, determining what each subtree should evaluate to in order to achieve the target result with minimum flips.

Input & Output

example_1.py โ€” Basic OR Operation
$ Input: root = [2,0,1], result = false Tree: OR(0,1)
โ€บ Output: 1
๐Ÿ’ก Note: The tree evaluates to true (0 OR 1 = true). To get false, we need both children false, so flip the right child: 1 flip needed.
example_2.py โ€” AND with XOR
$ Input: root = [3,4,[0,1],1], result = true Tree: AND(XOR(0,1),1)
โ€บ Output: 0
๐Ÿ’ก Note: XOR(0,1) = true, AND(true,1) = true. Already evaluates to true, so 0 flips needed.
example_3.py โ€” NOT Operation
$ Input: root = [5,0], result = true Tree: NOT(0)
โ€บ Output: 0
๐Ÿ’ก Note: NOT(0) = true. Already evaluates to the desired result, so 0 flips needed.

Visualization

Tap to expand
Digital Circuit Optimization AnalogyOUTPUTORAND0100Cost Analysis (Target: true)Left OR gate: Can be true with cost 0 (flip switch 0โ†’1 costs 1, but switch 1 is already true)Right AND gate: Needs both inputs true, cost = 2 (flip both 0โ†’1)Main gate (OR): Needs at least one input trueโ€ข Option 1: Left=true, Right=false โ†’ Cost = 0 + 0 = 0 โœ“โ€ข Option 2: Left=false, Right=true โ†’ Cost = 1 + 2 = 3โ€ข Option 3: Left=true, Right=true โ†’ Cost = 0 + 2 = 2Minimum flips needed: 0
Understanding the Visualization
1
Identify Structure
Map out the boolean operations tree with leaves as switches
2
Calculate Leaf Costs
For each switch, determine flip cost for true/false
3
Combine Gate Costs
Work upward, calculating optimal costs for each gate
4
Find Optimal Path
Choose the minimum cost path to desired output
Key Takeaway
๐ŸŽฏ Key Insight: By calculating costs for both true and false outcomes at each node, we can optimally combine paths and avoid exploring all exponential possibilities, reducing complexity from O(2^n) to O(n).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single traversal of tree with constant work per node

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack proportional to tree height

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is in the range [1, 105]
  • Node values: 0, 1 for leaves, 2, 3, 4, 5 for internal nodes
  • NOT nodes have exactly one child, others have exactly two children
  • Leaf nodes have values 0 or 1 only
  • It is guaranteed that result can always be achieved
Asked in
Google 45 Microsoft 32 Meta 28 Amazon 24
36.7K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen