※ 引述《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命題之真假值
: 思路:
: dfs
: Python Code:
: # Definition for a binary tree node.
: # class TreeNode:
: # def __init__(self, val=0, left=None, right=None):
: # self.val = val
: # self.left = left
: # self.right = right
: class Solution:
: def evaluateTree(self, root: Optional[TreeNode]) -> bool:
: def dfs(node):
: if node.val == 0 or node.val == 1:
: return node.val
: if node.val == 2:
: return dfs(node.left) or dfs(node.right)
: if node.val == 3:
: return dfs(node.left) and dfs(node.right)
: return dfs(root)
: 我是ez守門員
思路:遞迴
C# code
public class Solution {
public bool EvaluateTree(TreeNode root) {
if (root.val < 2) return root.val == 1;
if (root.val == 2) return EvaluateTree(root.left) || EvaluateTree(root.right);
return EvaluateTree(root.left) && EvaluateTree(root.right);
}
}
EASY守門員
昨天的狗幹題目看一看就放棄了