Evaluate Boolean Binary Tree

easy binary tree recursion

Problem

You are given a full binary tree (every node has 0 or 2 children) whose leaves hold 0 (false) or 1 (true), and whose inner nodes hold 2 (OR) or 3 (AND). A leaf evaluates to its own boolean; an inner node evaluates by applying its operation to the evaluations of its two children. Return the boolean value of the root.

Inputroot = [2,1,3,null,null,0,1]
Outputtrue
The AND node gives false AND true = false; the OR root then gives true OR false = true.

def evaluate_tree(root):
    if not root.left:                 # leaf node
        return root.val == 1
    left = evaluate_tree(root.left)
    right = evaluate_tree(root.right)
    if root.val == 2:                 # 2 means OR
        return left or right
    return left and right             # 3 means AND
function evaluateTree(root) {
  if (!root.left) {                  // leaf node
    return root.val === 1;
  }
  const left = evaluateTree(root.left);
  const right = evaluateTree(root.right);
  if (root.val === 2) return left || right;  // OR
  return left && right;                      // AND
}
public boolean evaluateTree(TreeNode root) {
    if (root.left == null) {           // leaf node
        return root.val == 1;
    }
    boolean left = evaluateTree(root.left);
    boolean right = evaluateTree(root.right);
    if (root.val == 2) return left || right;   // OR
    return left && right;                      // AND
}
bool evaluateTree(TreeNode* root) {
    if (!root->left) {                 // leaf node
        return root->val == 1;
    }
    bool left = evaluateTree(root->left);
    bool right = evaluateTree(root->right);
    if (root->val == 2) return left || right;  // OR
    return left && right;                      // AND
}
Time: O(n) Space: O(h)