Minimum Flips in Binary Tree to Get Result — LeetCode 2313 Python Solution

HardLeetCode PremiumTreeDepth-First SearchDynamic ProgrammingBinary Tree
Problem
#2313
Reading time
4 min

The 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.

Example

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

Python solution

Python
# 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)]

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Tree Traversal

Choose the order — preorder, inorder, postorder, level — and the problem solves itself. LeetCode 2313. Minimum Flips in Binary Tree to Get Result is filed here because LeetCode tags it Tree and Binary Tree, which is the vocabulary this hub collects.

The tree traversal guide has the Python template for the pattern and the 225 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2313. Minimum Flips in Binary Tree to Get Result?
LeetCode 2313. Minimum Flips in Binary Tree to Get Result is rated Hard on LeetCode.
What is the time complexity of LeetCode 2313. Minimum Flips in Binary Tree to Get Result?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2313. Minimum Flips in Binary Tree to Get Result?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 2313. Minimum Flips in Binary Tree to Get Result cover?
LeetCode 2313. Minimum Flips in Binary Tree to Get Result is tagged Tree, Depth-First Search, Dynamic Programming and Binary Tree on LeetCode.
Is LeetCode 2313. Minimum Flips in Binary Tree to Get Result a premium problem?
Yes. LeetCode 2313. Minimum Flips in Binary Tree to Get Result is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview