Popular Posts

Saturday, January 1, 2011

Logical gates algorithm

You have a tree(binary tree) having nodes such that all the internal nodes are either OR(bitwise '|' or bitwise AND '&') and all the leaves are either '0' or '1'.

eg:
AND
/ \
OR AND
/\ /\
AND 0 0 1
/\
0 0


Now we will be given a value which we need to get at root that can be either 0 or 1.
Now we need to find minimum number of gates we need to change in order to get the desired value at the root.

Value at any node is evaluated simply ='left->node' operation at node 'right->node'

like in this case if we need 1 at root then we require to change only one AND gate to OR gate.
ANS is 1.

No comments:

Post a Comment