Leaf Flip Results
GoogleOnsiteMedium
A boolean expression is stored as a tree written with nested lists. A leaf is a boolean (true or false). An inner node is a list that starts with an operator name: ["AND", left, right], ["OR", left, right], ["XOR", left, right] for two children, or ["NOT", child] for one child. XOR is true when exactly one child is true.
Part 1: Flip each leaf
You are given a boolean expression tree as described above.
For each leaf, from left to right, return the value of the whole expression when that leaf's value is flipped (true becomes false and the reverse) and every other leaf keeps its value. Each leaf is considered independently of the others, and the results are returned in leaf order.
The tree itself may be a single leaf, written as a bare true or false; flipping it gives the opposite value, so the result is a one-element list.
- Example 1
- tree
["XOR",["OR",false,true],["AND",true,["NOT",true]]]- Output
[true,false,true,false]
Why: The root is
true. Flipping the first leaf (false) leaves theORattrue, so the root staystrue. Flipping the second leaf (true) makes theORfalseand the rootfalse. Flipping the third leaf (true, left child of theAND) leaves theANDfalsebecause theNOTbeside it isfalse, so the root staystrue. Flipping the last leaf (the one underNOT) makes theNOTtrue, theANDtrueand the rootfalse.
Constraints
- The tree has at most
10^4nodes (leaves and operator nodes). - The tree has depth at most
400. - Operator names are exactly
AND,OR,XORandNOT, in capitals.