Minimum Flips in Binary Tree to Get Result Problem
Description
LeetCode Problem 2313.
You are given the root of a binary tree with the following properties:
- Leaf nodes have either the value
0or1, representingfalseandtruerespectively. - Non-leaf nodes have either the value
2,3,4, or5, representing the boolean operationsOR,AND,XOR, andNOT, respectively.
You are also given a boolean result, which is the desired result of the evaluation of the root node.
The evaluation of a node is as follows:
- If the node is a leaf node, the evaluation is the value of the node, i.e.
trueorfalse. - Otherwise, evaluate the node's children and apply the boolean operation of its value with the children's evaluations.
In one operation, you can flip a leaf node, which causes a false node to become true, and a true node to become false.
Return the minimum number of operations that need to be performed such that the evaluation of root yields result. It can be shown that there is always a way to achieve result.
A leaf node is a node that has zero children.
Note: NOT nodes have either a left child or a right child, but other non-leaf nodes have both a left child and a right child.
Example 1:

Input: root = [3,5,4,2,null,1,1,1,0], result = true
Output: 2
Explanation:
It can be shown that a minimum of 2 nodes have to be flipped to make the root of the tree
evaluate to true. One way to achieve this is shown in the diagram above.
Example 2:
Input: root = [0], result = false
Output: 0
Explanation:
The root of the tree already evaluates to false, so 0 nodes have to be flipped.
Constraints:
- The number of nodes in the tree is in the range
[1, 10^5]. 0 <= Node.val <= 5OR,AND, andXORnodes have2children.NOTnodes have1child.- Leaf nodes have a value of
0or1. - Non-leaf nodes have a value of
2,3,4, or5.
Solution
Approach: Tree DP
For each node compute a pair (to_false, to_true): the minimum number of leaf flips that makes the subtree evaluate to false, and to true. A leaf with value x gives (x, x ^ 1). For an inner node combine the children l and r by its operator.
- OR (2): false needs both false,
l[0] + r[0]; true needs at least one true, so the cheapest of the other three combinations. - AND (3): true needs both true,
l[1] + r[1]; false is the cheapest of the other three combinations. - XOR (4): equal children give false, different children give true, so take the cheaper of each pairing.
- NOT (5): it has a single child, so false is the child's
to_trueand true is the child'sto_false. A missing child counts as infinity, sominpicks the real one.
The answer is the entry of the root's pair selected by result.
Complexity: Time O(n), space O(h) for the recursion, where n is the number of nodes and h the height.
from math import inf
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minimumFlips(self, root: Optional[TreeNode], result: bool) -> int:
def dfs(root: Optional[TreeNode]) -> (int, int):
if root is None:
return inf, inf
x = root.val
if x in (0, 1):
return x, x ^ 1
l, r = dfs(root.left), dfs(root.right)
if x == 2:
return l[0] + r[0], min(l[0] + r[1], l[1] + r[0], l[1] + r[1])
if x == 3:
return min(l[0] + r[0], l[0] + r[1], l[1] + r[0]), l[1] + r[1]
if x == 4:
return min(l[0] + r[0], l[1] + r[1]), min(l[0] + r[1], l[1] + r[0])
return min(l[1], r[1]), min(l[0], r[0])
return dfs(root)[int(result)]