Skip to content

Commit a68ee71

Browse files
authored
Merge pull request #881 from 0xff-dev/2331
Add solution and test-cases for problem 2331
2 parents 09c8cb4 + 77dd676 commit a68ee71

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

leetcode/2301-2400/2331.Evaluate-Boolean-Binary-Tree/README.md

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [2331.Evaluate Boolean Binary Tree][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given the `root` of a **full binary tree** with the following properties:
75

8-
**Example 1:**
6+
- **Leaf nodes** have either the value `0` or `1`, where `0` represents `False` and `1` represents `True`.
7+
- **Non-leaf nodes** have either the value `2` or `3`, where `2` represents the boolean `OR` and `3` represents the boolean `AND`.
98

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
9+
The `evaluation` of a node is as follows:
10+
11+
- If the node is a leaf node, the evaluation is the **value** of the node, i.e. `True` or `False`.
12+
- Otherwise, **evaluate** the node's two children and **apply** the boolean operation of its value with the children's evaluations.
13+
14+
Return the boolean result of **evaluating** the `root` node.
15+
16+
A **full binary** tree is a binary tree where each node has either `0` or `2` children.
1417

15-
## 题意
16-
> ...
18+
A **leaf node** is a node that has zero children.
1719

18-
## 题解
20+
**Example 1:**
1921

20-
### 思路1
21-
> ...
22-
Evaluate Boolean Binary Tree
23-
```go
22+
![1](./example1drawio1.png)
23+
24+
```
25+
Input: root = [2,1,3,null,null,0,1]
26+
Output: true
27+
Explanation: The above diagram illustrates the evaluation process.
28+
The AND node evaluates to False AND True = False.
29+
The OR node evaluates to True OR False = True.
30+
The root node evaluates to True, so we return true.
2431
```
2532

33+
**Example 2:**
34+
35+
```
36+
Input: root = [0]
37+
Output: false
38+
Explanation: The root node is a leaf node and it evaluates to false, so we return false.
39+
```
2640

2741
## 结语
2842

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type TreeNode struct {
4+
Val int
5+
Left, Right *TreeNode
6+
}
7+
8+
func Solution(root *TreeNode) bool {
9+
if root == nil {
10+
return true
11+
}
12+
if root.Val == 0 {
13+
return false
14+
}
15+
if root.Val == 1 {
16+
return true
17+
}
18+
l := Solution(root.Left)
19+
r := Solution(root.Right)
20+
if root.Val == 2 {
21+
return l || r
22+
}
23+
return l && r
524
}

leetcode/2301-2400/2331.Evaluate-Boolean-Binary-Tree/Solution_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs *TreeNode
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &TreeNode{Val: 2, Left: &TreeNode{Val: 1}, Right: &TreeNode{Val: 3, Left: &TreeNode{Val: 0}, Right: &TreeNode{Val: 1}}}, true},
17+
{"TestCase", &TreeNode{Val: 0}, false},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}
Loading

0 commit comments

Comments
 (0)