※ 引述《sustainer123 (caster )》之銘言:
: https://leetcode.com/problems/evaluate-boolean-binary-tree
: 2331. Evaluate Boolean Binary Tree
: 給定滿二叉樹 root
: root.val == 0為False
: root.val == 1為True
: root.val == 2為or
: root.val == 3為and
: 請回傳root命題之真假值
雖然我很費 但easy還是可以的
C++ code:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class Solution {
public:
bool evaluateTree(TreeNode* root) {
if (root->val == 2) {
return evaluateTree(root->left) | evaluateTree(root->right);
}
else if(root->val == 3) {
return evaluateTree(root->left) & evaluateTree(root->right);
}
return root->val;
}
};