Evaluate Boolean Binary Tree

Easy Topics Company Tags

Evaluate the expression represented by a full binary tree and return its Boolean result. Every node has either no children or exactly two children. A leaf value of 0 means false, and 1 means true. An internal value of 2 applies OR to its children; an internal value of 3 applies AND.

Examples use level-order arrays. After the root, each present node contributes left and right child entries in queue order. null marks a missing child, and trailing missing entries may be omitted.

Example 1:

Evaluate Boolean Binary Tree, example 1: illustration of the example

Input: root = [3,1,2,null,null,0,1]

Output: true

Explanation: The right subtree computes false OR true, which is true. The root then computes true AND true, giving true.

Example 2:

Evaluate Boolean Binary Tree, example 2: illustration of the example

Input: root = [2,0,3,null,null,1,0]

Output: false

Explanation: The right subtree computes true AND false, which is false. The root computes false OR false, giving false.

Example 3:

Evaluate Boolean Binary Tree, example 3: illustration of the example

Input: root = [1]

Output: true

Explanation: The root is a leaf with value 1, so the expression is true.

Constraints:

  • The tree contains between 1 and 1000 nodes.
  • Every node has either zero or two children.
  • Leaves contain 0 or 1. Internal nodes contain 2 or 3.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.