Minimum Flips in Binary Tree to Get Result

Problem Description

Ah, the classic “Minimum Flips in Binary Tree to Get Result” problem! Imagine you’re at a party, and everyone is trying to decide whether to play charades or karaoke. You want to flip the mood from “let’s sing our hearts out” to “let’s act like fools” with the least amount of effort. In this problem, we have a binary tree where each node represents a logical operation (AND, OR, NOT, XOR) and we need to flip the nodes to achieve a desired boolean result.

So, if you think of each node as a party-goer with a specific preference, your job is to convince them to change their minds with the least number of flips. Sounds fun, right?

Code Solution


class Solution {
 public:
  int minimumFlips(TreeNode* root, bool result) {
    return dp(root, result);
  }

 private:
  struct PairHash {
    template 
    std::size_t operator()(const std::pair& p) const {
      return std::hash{}(p.first) ^ std::hash{}(p.second);
    }
  };

  unordered_map, int, PairHash> mem;

  int dp(TreeNode* root, bool target) {
    const pair key{root, target};
    if (const auto it = mem.find(key); it != mem.cend())
      return it->second;
    if (root->val == 0 || root->val == 1)  // the leaf
      return root->val == target ? 0 : 1;
    if (root->val == 5)  // NOT
      return dp(root->left == nullptr ? root->right : root->left, !target);

    vector> nextTargets;
    if (root->val == 2)  // OR
      nextTargets = target ? vector>{{0, 1}, {1, 0}, {1, 1}}
                           : vector>{{0, 0}};
    else if (root->val == 3)  // AND
      nextTargets = target ? vector>{{1, 1}}
                           : vector>{{0, 0}, {0, 1}, {1, 0}};
    else  // root.val == 4 (XOR)
      nextTargets = target ? vector>{{0, 1}, {1, 0}}
                           : vector>{{0, 0}, {1, 1}};

    int ans = INT_MAX;
    for (const auto& [leftTarget, rightTarget] : nextTargets)
      ans = min(ans, dp(root->left, leftTarget) + dp(root->right, rightTarget));
    return mem[key] = ans;
  }
};

Approach Explanation

The code uses a depth-first search (DFS) approach to traverse the binary tree. It employs memoization to store previously computed results for specific nodes and target values, which helps in reducing redundant calculations. The function dp recursively determines the minimum number of flips required to achieve the desired boolean result at each node, considering the logical operations defined by the node’s value.

Time and Space Complexity

Complexity Type Complexity
Time Complexity O(N * 2H)
Space Complexity O(N)

Real-World Example

Imagine you’re trying to convince your friends to switch from playing video games to board games. Each friend has a different preference (AND, OR, NOT, XOR) on what they want to play. You need to flip their preferences with the least amount of persuasion (flips) to get everyone on board with the board game idea. Just like in the binary tree, each friend’s preference can be thought of as a node, and your goal is to achieve a unified decision with minimal effort.

Similar Problems

If you enjoyed this problem, you might also like: