Skip to content

Commit 36b6ee6

Browse files
solves #2331: Evaluate Boolean Binary Tree in java
1 parent 9bc7dfa commit 36b6ee6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@
748748
| 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
749749
| 2319 | [Check if Matrix Is X-Matrix](https://leetcode.com/problems/check-if-matrix-is-x-matrix) | [![Java](assets/java.png)](src/CountAsterisks.java) | |
750750
| 2325 | [Decode the Message](https://leetcode.com/problems/decode-the-message) | [![Java](assets/java.png)](src/DecodeTheMessage.java) | |
751-
| 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree) | | |
751+
| 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree) | [![Java](assets/java.png)](src/EvaluateBooleanBinaryTree.java) | |
752752
| 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups) | | |
753753
| 2439 | [Minimize Maximum of Array](https://leetcode.com/problems/minimize-maximum-of-array) | [![Python](assets/python.png)](python/minimize_maximum_of_array.py) | |
754754
| 2341 | [Maximum Number of Pairs in Array](https://leetcode.com/problems/maximum-number-of-pairs-in-array) | | |

Diff for: src/EvaluateBooleanBinaryTree.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://leetcode.com/problems/evaluate-boolean-binary-tree
2+
// T: O(N)
3+
// S: O(log(N))
4+
5+
public class EvaluateBooleanBinaryTree {
6+
public boolean evaluateTree(TreeNode root) {
7+
if (isLeafNode(root)) {
8+
return root.val == 1;
9+
}
10+
11+
if (root.val == 2) {
12+
return evaluateTree(root.left) || evaluateTree(root.right);
13+
}
14+
15+
return evaluateTree(root.left) && evaluateTree(root.right);
16+
}
17+
18+
private boolean isLeafNode(TreeNode root) {
19+
return root.left == null && root.right == null;
20+
}
21+
}

0 commit comments

Comments
 (0)