(This question is a variation of the LeetCode question 2331. Evaluate Boolean Binary Tree. If you haven't completed that question yet, it is recommended to solve it first.)
A Boolean expression tree represents Boolean values at its leaves and Boolean operations at its internal nodes. You are given the root of a valid expression tree. Each node is a TreeNode with an integer val and two child fields, TreeNode left and TreeNode right. Nodes have no parent pointers.
Node values are encoded as follows:
0:falseleaf.1:trueleaf.2:OR.3:AND.4:XOR.5:NOT.
AND, OR, and XOR nodes each have two children, while a NOT node has one child stored on the left. Leaves have no children.
The tree is evaluated using the corresponding Boolean operations; expression parsing is not required.
Each leaf is flipped independently, with its original value restored before the next leaf is considered. The result contains the resulting root value for each flip in left-to-right leaf order. No separate result is returned for the original, unmodified tree.
Return the array of resulting root values.
Constraints:
- The tree contains between 1 and 1,000 nodes.
rootis non-null, and the nodes form a tree with no shared children.
Example 1:
Input: root = [4, 2, 3, 0, 1, 1, 5, null, null, null, null, null, null, 1]
4 / \ 2 3 / \ / \ 0 1 1 5 / 1Output: [true, false, true, false] Explanation: The original expression is (false OR true) XOR (true AND NOT true), which evaluates to true. Flip each leaf independently, from left to right:
- Flip the first leaf from false to true: (true OR true) XOR (true AND NOT true) evaluates to true.
- Flip the second leaf from true to false: (false OR false) XOR (true AND NOT true) evaluates to false.
- Flip the third leaf from true to false: (false OR true) XOR (false AND NOT true) evaluates to true.
- Flip the fourth leaf from true to false: (false OR true) XOR (true AND NOT false) evaluates to false.
Example 2:
Input: root = [3, 4, 2, 5, 1, 0, 1, 0]
3 / \ 4 2 / \ / \ 5 1 0 1 / 0Output: [true, true, false, false]
Example 3:
Input: root = [4, 0, 1]
4 / \ 0 1Output: [false, false]
