rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

2331. 计算布尔二叉树的值 #69

Open rocksc30 opened 1 year ago

rocksc30 commented 1 year ago
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean evaluateTree(TreeNode root) {
        if(root.left == null && root.right == null && root.val == 1){
            return true;
        }else if(root.left == null && root.right == null && root.val == 0){
            return false;
        }
        if(root.val == 2){
            return evaluateTree(root.left) || evaluateTree(root.right);
        }
        if(root.val == 3){
            return evaluateTree(root.left) && evaluateTree(root.right);
        }
        return false;
    }
}