aboutsummaryrefslogtreecommitdiff
path: root/evaluate_boolean_binary_tree.py
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /evaluate_boolean_binary_tree.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'evaluate_boolean_binary_tree.py')
-rw-r--r--evaluate_boolean_binary_tree.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/evaluate_boolean_binary_tree.py b/evaluate_boolean_binary_tree.py
new file mode 100644
index 0000000..cc9237a
--- /dev/null
+++ b/evaluate_boolean_binary_tree.py
@@ -0,0 +1,36 @@
+# 2331. Evaluate Boolean Binary Tree
+
+"""
+given the 'root' of a full binary tree with the following properties. leaf
+nodes have either the value 0 or 1, where 0 represents false and 1 represents
+true. non leaf nodes have either the value 2 or 3, where 2 represents the
+boolean or, and 3 represents the boolean and. the evaluation of a node is as
+follows. if the node is a leaf node, the evaluation is the value of the node,
+ie. true or false. otherwise, evaluate the node's two children and apply the
+boolean operation of its value with the children's evaluations. return the
+boolean result of evaulating the 'root' node. a full binary tree is a binary
+tree where each node has either 0 or 2 children. a leaf node is a node that
+has zero children.
+"""
+
+
+# Definition for a binary tree node.
+class TreeNode(object):
+ def __init__(self, val=0, left=None, right=None):
+ self.val = val
+ self.left = left
+ self.right = right
+
+
+class Solution(object):
+ def evaluateTree(self, root):
+ """
+ :type root: Optional[TreeNode]
+ :rtype: bool
+ """
+ if root.val == 0 or root.val == 1:
+ return root.val
+ if root.val == 2:
+ return self.evaluateTree(root.left) or self.evaluateTree(root.right)
+ if root.val == 3:
+ return self.evaluateTree(root.left) and self.evaluateTree(root.right)