You are given the root of a binary tree with the following properties:
0 or 1, representing false and true respectively.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:
true or false.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:
[1, 105].0 <= Node.val <= 5OR, AND, and XOR nodes have 2 children.NOT nodes have 1 child.0 or 1.2, 3, 4, or 5.We define a function dfs(root), which returns an array of length 2. The first element represents the minimum number of flips needed to change the value of the root node to false, and the second element represents the minimum number of flips needed to change the value of the root node to true. The answer is dfs(root)[result].
The implementation of the function dfs(root) is as follows:
If root is null, return [+infty, +infty].
Otherwise, let x be the value of root, l be the return value of the left subtree, and r be the return value of the right subtree. Then we discuss the following cases:
x \in {0, 1}, return [x, x \oplus 1].x = 2, which means the boolean operator is OR, to make the value of root false, we need to make both the left and right subtrees false. Therefore, the first element of the return value is l[0] + r[0]. To make the value of root true, we need at least one of the left or right subtrees to be true. Therefore, the second element of the return value is min(l[0] + r[1], l[1] + r[0], l[1] + r[1]).x = 3, which means the boolean operator is AND, to make the value of root false, we need at least one of the left or right subtrees to be false. Therefore, the first element of the return value is min(l[0] + r[0], l[0] + r[1], l[1] + r[0]). To make the value of root true, we need both the left and right subtrees to be true. Therefore, the second element of the return value is l[1] + r[1].x = 4, which means the boolean operator is XOR, to make the value of root false, we need both the left and right subtrees to be either false or true. Therefore, the first element of the return value is min(l[0] + r[0], l[1] + r[1]). To make the value of root true, we need the left and right subtrees to be different. Therefore, the second element of the return value is min(l[0] + r[1], l[1] + r[0]).x = 5, which means the boolean operator is NOT, to make the value of root false, we need at least one of the left or right subtrees to be true. Therefore, the first element of the return value is min(l[1], r[1]). To make the value of root true, we need at least one of the left or right subtrees to be false. Therefore, the second element of the return value is min(l[0], r[0]).The time complexity is O(n), and the space complexity is O(n). Where n is the number of nodes in the binary tree.
Java
C++
Go
TypeScript
Leetcode questions IRL 🙃 • Alberta Tech • 986,923 views views
Watch 9 more video solutions →Practice Minimum Flips in Binary Tree to Get Result with our built-in code editor and test cases.
Practice on FleetCode