2331. Evaluate Boolean Binary Tree LeetCode Solution
In this guide, you will get 2331. Evaluate Boolean Binary Tree LeetCode Solution with the best time and space complexity. The solution to Evaluate Boolean Binary Tree problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Evaluate Boolean Binary Tree solution in C++
- Evaluate Boolean Binary Tree solution in Java
- Evaluate Boolean Binary Tree solution in Python
- Additional Resources
Problem Statement of Evaluate Boolean Binary Tree
You are 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, i.e. 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 evaluating 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.
Example 1:
Input: root = [2,1,3,null,null,0,1]
Output: true
Explanation: The above diagram illustrates the evaluation process.
The AND node evaluates to False AND True = False.
The OR node evaluates to True OR False = True.
The root node evaluates to True, so we return true.
Example 2:
Input: root = [0]
Output: false
Explanation: The root node is a leaf node and it evaluates to false, so we return false.
Constraints:
The number of nodes in the tree is in the range [1, 1000].
0 <= Node.val <= 3
Every node has either 0 or 2 children.
Leaf nodes have a value of 0 or 1.
Non-leaf nodes have a value of 2 or 3.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(h)
2331. Evaluate Boolean Binary Tree LeetCode Solution in C++
class Solution {
public:
bool evaluateTree(TreeNode* root) {
if (root->val < 2)
return root->val;
if (root->val == 2) // OR
return evaluateTree(root->left) || evaluateTree(root->right);
// AND
return evaluateTree(root->left) && evaluateTree(root->right);
}
};
/* code provided by PROGIEZ */
2331. Evaluate Boolean Binary Tree LeetCode Solution in Java
class Solution {
public boolean evaluateTree(TreeNode root) {
if (root.val < 2)
return root.val == 1;
if (root.val == 2) // OR
return evaluateTree(root.left) || evaluateTree(root.right);
// AND
return evaluateTree(root.left) && evaluateTree(root.right);
}
}
// code provided by PROGIEZ
2331. Evaluate Boolean Binary Tree LeetCode Solution in Python
class Solution:
def evaluateTree(self, root: TreeNode | None) -> bool:
if root.val < 2:
return root.val
if root.val == 2: # OR
return self.evaluateTree(root.left) or self.evaluateTree(root.right)
# AND
return self.evaluateTree(root.left) and self.evaluateTree(root.right)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.