aboutsummaryrefslogtreecommitdiff
path: root/evaluate_boolean_binary_tree.c
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.c
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'evaluate_boolean_binary_tree.c')
-rw-r--r--evaluate_boolean_binary_tree.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/evaluate_boolean_binary_tree.c b/evaluate_boolean_binary_tree.c
new file mode 100644
index 0000000..14e71e4
--- /dev/null
+++ b/evaluate_boolean_binary_tree.c
@@ -0,0 +1,39 @@
+// 2331. Evaluate Boolean Binary Tree
+#include "leetcode.h"
+
+/*
+ * 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.
+ */
+
+struct TreeNode {
+ int val;
+ struct TreeNode *left;
+ struct TreeNode *right;
+};
+
+bool evaluateTree(struct TreeNode *root) {
+ if (!root)
+ return true;
+ bool l = evaluateTree(root->left), r = evaluateTree(root->right);
+ switch (root->val) {
+ case 2:
+ root->val = l || r;
+ return l || r;
+ case 3:
+ root->val = l && r;
+ return l && r;
+ case 0:
+ return false;
+ default:
+ return true;
+ }
+}