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

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

计算布尔二叉树的值【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;


。确定单层递归的逻辑


  • 当节点是叶子节点时,根据值返回true和false
  • 当节点不是叶子节点使,继续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 )


  • 优化:不需要记录父节点的值,二叉树为完整二叉树


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 )
目录
相关文章
|
算法 API 计算机视觉
图像特征提取与描述_角点特征01:Harris算法+Shi-Tomas算法
Harris角点检测的思想是通过图像的局部的小窗口观察图像,角点的特征是窗口沿任意方向移动都会导致图像灰度的明显变化
1033 0
洛谷 P2634 BZOJ 2152 【模板】点分治(聪聪可可)
题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃、两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已经玩儿腻了这种低智商的游戏。
1101 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8043 0
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17925 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
1天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
748 0
|
7天前
|
云安全 数据采集 人工智能
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1445 5