2331. Evaluate Boolean Binary Tree

Photo by niko photos on Unsplash

2331. Evaluate Boolean Binary Tree

class Solution:
    def evaluateTree(self, root: Optional[TreeNode]) -> bool:
        def travel(node):
            if node:                
                leftVal = travel(node.left)
                rightVal = travel(node.right)

                return node.val if not node.left else (leftVal or rightVal) if node.val == 2 else (leftVal and rightVal)

        return travel(root)