Skip to content

Commit d1c13c7

Browse files
committed
2331. 计算布尔二叉树的值
1 parent 07dc28a commit d1c13c7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
|2309|[兼具大小写的最好英文字母](https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/)|[JavaScript](./algorithms/greatest-english-letter-in-upper-and-lower-case.js)|Easy|
213213
|2315|[统计星号](https://leetcode.cn/problems/count-asterisks/)|[JavaScript](./algorithms/count-asterisks.js)|Easy|
214214
|2319|[判断矩阵是否是一个 X 矩阵](https://leetcode.cn/problems/check-if-matrix-is-x-matrix/)|[JavaScript](./algorithms/check-if-matrix-is-x-matrix.js)|Easy|
215+
|2331|[计算布尔二叉树的值](https://leetcode.cn/problems/evaluate-boolean-binary-tree/)|[JavaScript](./algorithms/evaluate-boolean-binary-tree.js)|Easy|
215216
|2351|[第一个出现两次的字母](https://leetcode.cn/problems/first-letter-to-appear-twice/)|[JavaScript](./algorithms/first-letter-to-appear-twice.js)|Easy|
216217
|面试题 04.12|[面试题 04.12. 求和路径](https://leetcode.cn/problems/paths-with-sum-lcci/)|[JavaScript](./algorithms/paths-with-sum-lcci.js)|Medium|
217218
|面试题 02.07|[面试题 02.07. 链表相交](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/)|[JavaScript](./algorithms/intersection-of-two-linked-lists-lcci.js)|Easy|
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* 2331. 计算布尔二叉树的值
11+
* @param {TreeNode} root
12+
* @return {boolean}
13+
*/
14+
var evaluateTree = function(root) {
15+
if (!root) return false;
16+
if (root.val < 2) {
17+
return !!root.val;
18+
}
19+
const left = evaluateTree(root.left);
20+
const right = evaluateTree(root.right);
21+
if (root.val === 2) {
22+
return left || right;
23+
}
24+
return left && right;
25+
};

0 commit comments

Comments
 (0)