Ctrl K

Logic Gate Tree

Easy
A logic circuit is stored as a binary tree with root root. Every node has either no children or exactly two. - A leaf is an input of the circuit: 0 stands for false and 1 for true. - An inner node is a gate: 2 means OR and 3 means AND. A gate applies its operation to the values of its left and right subtrees. Return the output of the circuit, which is the value of the root, as True or False.
Example 1
root=tree_node([3, 2, 1, 0, 1]) → True

The left node is an OR of 0 and 1, which is true. The root is an AND of that and the right leaf 1, so it is true as well.

Example 2
root=tree_node([3, 1, 2, None, None, 0, 0]) → False

The right node is an OR of two zeros, which is false. The root is an AND with a false input, so the output is false.

Example 3
root=tree_node([1]) → True

A circuit with a single input simply outputs that input.

Constraints
  • 1 ≤ number of nodes ≤ 1000
  • Every node has 0 or 2 children
  • A leaf holds 0 or 1
  • An inner node holds 2 or 3
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLeft, Node, Right EasyIdentical Trees EasyMirror-Image Tree EasyTree Height EasyEvenly Grown Tree Easy
loading Python…
Input
root=tree_node([3, 2, 1, 0, 1])
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 20 tests on the server, hidden ones included.