【每日一题Day110】LC2331计算布尔二叉树的值 | dfs

给定一棵满二叉树,其中叶节点的值为0或1,分别代表False和True,非叶节点的值为2或3,分别代表OR和AND。文章描述了一个深度优先搜索(DFS)的解决方案,用于递归地计算树中每个节点的布尔值,最终返回根节点的计算结果。时间复杂度和空间复杂度均为O(n),其中n为树的节点数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

计算布尔二叉树的值【LC2331】

You are given the root of a full binary tree with the following properties:

  • Leaf nodes have either the value 0 or 1, where 0 represents False and 1 represents True.
  • Non-leaf nodes have either the value 2 or 3, where 2 represents the boolean OR and 3 represents the boolean AND.

The evaluation of a node is as follows:

  • If the node is a leaf node, the evaluation is the value of the node, i.e. True or False.
  • Otherwise, evaluate the node’s two children and apply the boolean operation of its value with the children’s evaluations.

Return the boolean result of evaluating the root node.

A full binary tree is a binary tree where each node has either 0 or 2 children.

A leaf node is a node that has zero children.

吃饱饱喝饱饱学习习

  • 思路:DFS

    使用深度搜索,返回每颗子树的布尔值,每颗子树的布尔结果与其布尔操作有关,即父节点的值,因此递归函数需要传入父节点的值

  • 实现

    • 确定递归函数的参数和返回值

      • 参数:当前节点root,父节点的值

      • 返回值:boolean,该子树的计算结果

    • 确定终止条件

      • 当前节点是空时返回,当父节点为或操作时,返回false;当父节点为与操作时,返回true
    • 确定单层递归的逻辑

      • 当节点是叶子节点时,根据值返回truefalse
      • 当节点不是叶子节点使,继续dfs搜索
    /**
     * 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) {
            return dfs(root, 2);
        }
        public boolean dfs (TreeNode root, int pre){
            if (root == null){
                if (pre == 2) return false;// or
                else return true;// and
            }
            if (root.val == 2) {
                return dfs(root.left, 2) || dfs(root.right, 2);
            }
            else if (root.val == 3){
                return dfs(root.left, 3) && dfs(root.right, 3);
            }
            return root.val == 1;
        }
    
    }
    
    • 复杂度
      • 时间复杂度:O(n)O(n)O(n)
      • 空间复杂度:O(n)O(n)O(n)
  • 优化:不需要记录父节点的值,二叉树为完整二叉树

    class Solution {
        public boolean evaluateTree(TreeNode root) {
            if (root.left == null) {
                return root.val == 1;
            } 
            if (root.val == 2) {
                return evaluateTree(root.left) || evaluateTree(root.right);
            } else {
                return evaluateTree(root.left) && evaluateTree(root.right);
            }
        }
    }
    
    作者:力扣官方题解
    链接:https://leetcode.cn/problems/evaluate-boolean-binary-tree/solutions/2091770/ji-suan-bu-er-er-cha-shu-de-zhi-by-leetc-4g8f/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
    • 复杂度
      • 时间复杂度:O(n)O(n)O(n)
      • 空间复杂度:O(n)O(n)O(n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值