Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit deda2fe

Browse files
committed
feat: add solutions to lc problem: No.2331
No.2331.Evaluate Boolean Binary Tree
1 parent be520ca commit deda2fe

File tree

5 files changed

+185
-6
lines changed

5 files changed

+185
-6
lines changed

‎solution/2300-2399/2331.Evaluate Boolean Binary Tree/README.md‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ func evaluateTree(root *TreeNode) bool {
302302

303303
function evaluateTree(root: TreeNode | null): boolean {
304304
const { val, left, right } = root;
305-
if (left == null&&right==null) {
306-
return !!val;
305+
if (left == null) {
306+
return val===1;
307307
}
308308
if (val === 2) {
309309
return evaluateTree(left) || evaluateTree(right);
@@ -312,6 +312,69 @@ function evaluateTree(root: TreeNode | null): boolean {
312312
}
313313
```
314314

315+
### **Rust**
316+
317+
```rust
318+
// Definition for a binary tree node.
319+
// #[derive(Debug, PartialEq, Eq)]
320+
// pub struct TreeNode {
321+
// pub val: i32,
322+
// pub left: Option<Rc<RefCell<TreeNode>>>,
323+
// pub right: Option<Rc<RefCell<TreeNode>>>,
324+
// }
325+
//
326+
// impl TreeNode {
327+
// #[inline]
328+
// pub fn new(val: i32) -> Self {
329+
// TreeNode {
330+
// val,
331+
// left: None,
332+
// right: None
333+
// }
334+
// }
335+
// }
336+
use std::rc::Rc;
337+
use std::cell::RefCell;
338+
impl Solution {
339+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
340+
let root = root.as_ref().unwrap().as_ref().borrow();
341+
if root.left.is_none() {
342+
return root.val == 1;
343+
}
344+
if root.val == 2 {
345+
return Self::dfs(&root.left) || Self::dfs(&root.right);
346+
}
347+
Self::dfs(&root.left) && Self::dfs(&root.right)
348+
}
349+
350+
pub fn evaluate_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
351+
Self::dfs(&root)
352+
}
353+
}
354+
```
355+
356+
### **C**
357+
358+
```c
359+
/**
360+
* Definition for a binary tree node.
361+
* struct TreeNode {
362+
* int val;
363+
* struct TreeNode *left;
364+
* struct TreeNode *right;
365+
* };
366+
*/
367+
bool evaluateTree(struct TreeNode *root) {
368+
if (!root->left) {
369+
return root->val == 1;
370+
}
371+
if (root->val == 2) {
372+
return evaluateTree(root->left) || evaluateTree(root->right);
373+
}
374+
return evaluateTree(root->left) && evaluateTree(root->right);
375+
}
376+
```
377+
315378
### **...**
316379
317380
```

‎solution/2300-2399/2331.Evaluate Boolean Binary Tree/README_EN.md‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ func evaluateTree(root *TreeNode) bool {
279279

280280
function evaluateTree(root: TreeNode | null): boolean {
281281
const { val, left, right } = root;
282-
if (left == null&&right==null) {
283-
return !!val;
282+
if (left == null) {
283+
return val===1;
284284
}
285285
if (val === 2) {
286286
return evaluateTree(left) || evaluateTree(right);
@@ -289,6 +289,69 @@ function evaluateTree(root: TreeNode | null): boolean {
289289
}
290290
```
291291

292+
### **Rust**
293+
294+
```rust
295+
// Definition for a binary tree node.
296+
// #[derive(Debug, PartialEq, Eq)]
297+
// pub struct TreeNode {
298+
// pub val: i32,
299+
// pub left: Option<Rc<RefCell<TreeNode>>>,
300+
// pub right: Option<Rc<RefCell<TreeNode>>>,
301+
// }
302+
//
303+
// impl TreeNode {
304+
// #[inline]
305+
// pub fn new(val: i32) -> Self {
306+
// TreeNode {
307+
// val,
308+
// left: None,
309+
// right: None
310+
// }
311+
// }
312+
// }
313+
use std::rc::Rc;
314+
use std::cell::RefCell;
315+
impl Solution {
316+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
317+
let root = root.as_ref().unwrap().as_ref().borrow();
318+
if root.left.is_none() {
319+
return root.val == 1;
320+
}
321+
if root.val == 2 {
322+
return Self::dfs(&root.left) || Self::dfs(&root.right);
323+
}
324+
Self::dfs(&root.left) && Self::dfs(&root.right)
325+
}
326+
327+
pub fn evaluate_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
328+
Self::dfs(&root)
329+
}
330+
}
331+
```
332+
333+
### **C**
334+
335+
```c
336+
/**
337+
* Definition for a binary tree node.
338+
* struct TreeNode {
339+
* int val;
340+
* struct TreeNode *left;
341+
* struct TreeNode *right;
342+
* };
343+
*/
344+
bool evaluateTree(struct TreeNode *root) {
345+
if (!root->left) {
346+
return root->val == 1;
347+
}
348+
if (root->val == 2) {
349+
return evaluateTree(root->left) || evaluateTree(root->right);
350+
}
351+
return evaluateTree(root->left) && evaluateTree(root->right);
352+
}
353+
```
354+
292355
### **...**
293356
294357
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
bool evaluateTree(struct TreeNode *root) {
10+
if (!root->left) {
11+
return root->val == 1;
12+
}
13+
if (root->val == 2) {
14+
return evaluateTree(root->left) || evaluateTree(root->right);
15+
}
16+
return evaluateTree(root->left) && evaluateTree(root->right);
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
23+
let root = root.as_ref().unwrap().as_ref().borrow();
24+
if root.left.is_none() {
25+
return root.val == 1;
26+
}
27+
if root.val == 2 {
28+
return Self::dfs(&root.left) || Self::dfs(&root.right);
29+
}
30+
Self::dfs(&root.left) && Self::dfs(&root.right)
31+
}
32+
33+
pub fn evaluate_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
34+
Self::dfs(&root)
35+
}
36+
}

‎solution/2300-2399/2331.Evaluate Boolean Binary Tree/Solution.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
function evaluateTree(root: TreeNode | null): boolean {
1616
const { val, left, right } = root;
17-
if (left == null&&right==null) {
18-
return !!val;
17+
if (left == null) {
18+
return val===1;
1919
}
2020
if (val === 2) {
2121
return evaluateTree(left) || evaluateTree(right);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /