Problem

You are given the root of a binary tree with the following properties:

  • Leaf nodes have either the value 0 or 1, representing false and true respectively.
  • Non-leaf nodes have either the value 2, 3, 4, or 5, representing the boolean operations OR, AND, XOR, and NOT, 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. true or false.
  • 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 ofroot yieldsresult. 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.

Examples

Example 1:

1
2
3
4
5
6
![](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2313.Minimum%20Flips%20in%20Binary%20Tree%20to%20Get%20Result/images/operationstree.png)
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:

1
2
3
4
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, 105].
  • 0 <= Node.val <= 5
  • OR, AND, and XOR nodes have 2 children.
  • NOT nodes have 1 child.
  • Leaf nodes have a value of 0 or 1.
  • Non-leaf nodes have a value of 2, 3, 4, or 5.

Solution

Method 1 – DFS with Dynamic Programming

Intuition

We recursively compute the minimum flips needed for each subtree to achieve both True and False results. For each node, we consider all possible ways to flip its value or its children’s values to achieve the desired result, using the operation type at the node.

Approach

  1. For each node, recursively compute the minimum flips needed to make its subtree evaluate to True and False.
  2. For leaf nodes, flips are needed only if their value does not match the target.
  3. For non-leaf nodes, use the operation type to combine the results from children, considering all possible combinations and flips.
  4. Use memoization to avoid recomputation.
  5. Return the minimum flips needed for the root to achieve the desired result.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
struct TreeNode {
    int val;
    TreeNode *left, *right;
};
class Solution {
public:
    unordered_map<TreeNode*, array<int,2>> dp;
    int dfs(TreeNode* node) {
        if (!node) return 0;
        if (node->val == 0 || node->val == 1) {
            dp[node][node->val] = 0;
            dp[node][1-node->val] = 1;
            return 0;
        }
        if (dp.count(node)) return 0;
        dp[node] = {1e9, 1e9};
        if (node->val == 2) { // OR
            for (int a = 0; a < 2; ++a)
                for (int b = 0; b < 2; ++b) {
                    int res = a | b;
                    dp[node][res] = min(dp[node][res], dp[node->left][a] + dp[node->right][b]);
                }
        } else if (node->val == 3) { // AND
            for (int a = 0; a < 2; ++a)
                for (int b = 0; b < 2; ++b) {
                    int res = a & b;
                    dp[node][res] = min(dp[node][res], dp[node->left][a] + dp[node->right][b]);
                }
        } else if (node->val == 4) { // XOR
            for (int a = 0; a < 2; ++a)
                for (int b = 0; b < 2; ++b) {
                    int res = a ^ b;
                    dp[node][res] = min(dp[node][res], dp[node->left][a] + dp[node->right][b]);
                }
        } else if (node->val == 5) { // NOT
            for (int a = 0; a < 2; ++a) {
                int res = 1 - a;
                dp[node][res] = min(dp[node][res], dp[node->left ? node->left : node->right][a]);
            }
        }
        return 0;
    }
    int minimumFlips(TreeNode* root, bool result) {
        dfs(root);
        return dp[root][result];
    }
};
1
// Omitted for brevity, similar logic as C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
    public int minimumFlips(TreeNode root, boolean result) {
        int[] res = dfs(root);
        return result ? res[1] : res[0];
    }
    private int[] dfs(TreeNode node) {
        if (node == null) return new int[]{0, 0};
        if (node.val == 0) return new int[]{0, 1};
        if (node.val == 1) return new int[]{1, 0};
        int[] left = node.left != null ? dfs(node.left) : null;
        int[] right = node.right != null ? dfs(node.right) : null;
        int[] dp = new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE};
        if (node.val == 2) { // OR
            for (int a = 0; a < 2; a++)
                for (int b = 0; b < 2; b++)
                    dp[a|b] = Math.min(dp[a|b], left[a] + right[b]);
        } else if (node.val == 3) { // AND
            for (int a = 0; a < 2; a++)
                for (int b = 0; b < 2; b++)
                    dp[a&b] = Math.min(dp[a&b], left[a] + right[b]);
        } else if (node.val == 4) { // XOR
            for (int a = 0; a < 2; a++)
                for (int b = 0; b < 2; b++)
                    dp[a^b] = Math.min(dp[a^b], left[a] + right[b]);
        } else if (node.val == 5) { // NOT
            for (int a = 0; a < 2; a++)
                dp[1-a] = Math.min(dp[1-a], left != null ? left[a] : right[a]);
        }
        return dp;
    }
}
1
// Omitted for brevity, similar logic as Java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def minimum_flips(root: 'TreeNode', result: bool) -> int:
    def dfs(node):
        if not node:
            return [0, 0]
        if node.val == 0:
            return [0, 1]
        if node.val == 1:
            return [1, 0]
        left = dfs(node.left) if node.left else None
        right = dfs(node.right) if node.right else None
        dp = [float('inf'), float('inf')]
        if node.val == 2: # OR
            for a in range(2):
                for b in range(2):
                    dp[a|b] = min(dp[a|b], left[a] + right[b])
        elif node.val == 3: # AND
            for a in range(2):
                for b in range(2):
                    dp[a&b] = min(dp[a&b], left[a] + right[b])
        elif node.val == 4: # XOR
            for a in range(2):
                for b in range(2):
                    dp[a^b] = min(dp[a^b], left[a] + right[b])
        elif node.val == 5: # NOT
            for a in range(2):
                dp[1-a] = min(dp[1-a], (left if left else right)[a])
        return dp
    return dfs(root)[int(result)]
1
// Omitted for brevity, similar logic as C++
1
// Omitted for brevity, similar logic as JavaScript

Complexity

  • ⏰ Time complexity: O(n), where n is the number of nodes. Each node is visited once and each operation is constant time.
  • 🧺 Space complexity: O(n), for recursion stack and memoization.