leetcode 814. Binary Tree Pruning(修剪二叉树)

手把手教你用芯驰X9HP驱动MAX96755/MAX96752点亮智能座舱仪表屏(附完整寄存器配置表) 本文详细介绍了如何使用芯驰X9HP驱动MAX96755/MAX96752实现智能座舱仪表屏的完整开发流程,包括硬件架构解析、关键寄存器配置、Linux BSP集成及调试技巧。通过实战指南和完整寄存器配置表,帮助开发者快速掌握驱动开发核心技术,提升智能座舱系统的显示性能与稳定性。 阅读详情

We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1.

Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.

(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)

Example 1:
Input: [1,null,0,0,1]
Output: [1,null,0,null,1]

Explanation:
Only the red nodes satisfy the property “every subtree not containing a 1”.
The diagram on the right represents the answer.
在这里插入图片描述
把节点全是0的子树删掉

思路:
在遍历的过程中删掉全是0的子树

刚开始想把子树传入,当值全是0时设置root为null,但是发现通过参数把root传入,在函数中把root=null后,函数外面它还是存在的,如下面这样测试了一下,发现结果是not null。

	public static void main(String[] args) {
		TreeNode root = new TreeNode(1);
		root.left = new TreeNode(2);

		setNull(root.left);
		if(root.left == null) {
			System.out.println("null");
		} else {
			System.out.println("not null");
		}
	}
	
	static void setNull(TreeNode root) {
		root = null;
	}

所以只能判断子树中是否有1,没有1的话设置子树为null
而且不能看见1就返回true,还要遍历下去,把没有1的子树都删掉
注意root也是判断的对象,root是0,子树都是0,那么连root也要删掉

    public TreeNode pruneTree(TreeNode root) {
        if(root == null) return null;
        if(!hasOne(root)) root = null;
        return root;
    }
    
    boolean hasOne(TreeNode root) {
        if(root == null) return false;
        boolean leftOne = hasOne(root.left);
        boolean rightOne = hasOne(root.right);
        
        if(!leftOne) root.left = null;
        if(!rightOne) root.right = null;
        
        if(leftOne || rightOne || root.val == 1) return true;
                
        return false;
    }
Unity - gamma space下还原linear space效果 还有头发之后的 (头发没有处理完整,因为使用 ASE 连连看练出来的,使用 surface shader,虽然可以生成一下 vert, frag 的方式在修改,但是我懒得去修改了,这样就是 PBR 的 BRDF 里面的部分曲线是不一样的,所以可以看到头发有一些差异)其实上面的代码判断 是否有分量 > 1.0f 的方式来判断是否 HDR 是不太合理的,因为不同的贴图格式的编码方式不同。还有比较隐蔽的一些颜色相关的常量值 (比如PBR中的 绝缘体的 F0 常量值,等等) 阅读详情

相关推荐

Prometheus-2.53.1安装

Prometheus+Grafana安装

qlj062127的博客 1794

669. 修剪二叉搜索树(BST)

669. 修剪二叉搜索树题目解题思路代码 题目 给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树不应该改变保留在树中的元素的相对结构(即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在唯一的答案。 所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。 提示: 树中节点数在范围 [1, 104] 内 0 <= Node.val <=

Hello World 401

中国银行信息科技岗位笔试

收到笔试只剩四天了,收集的资料,后续会更新

weixin_46050242的博客 4617

【leetcode-medium】814. Binary Tree Pruning【修剪二叉樹】【自底向上反馈、修剪】

题目 We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. Return the same tree where every subtree (of the given tree) not containing a 1 has b...

课桌 542

React项目实战:用AntV G6打造可折叠组织架构图(附完整代码)

本文详细介绍了如何在React项目中使用AntV G6库构建一个功能完整的可折叠组织架构图。通过实战代码演示了从环境搭建、自定义节点、折叠状态管理到性能优化的全流程,重点解决了企业级应用中处理复杂树形数据、实现流畅交互等核心问题,并提供了可直接复用的组件代码。

weixin_29016315的博客 321

华为海思IC前端中后端(COT&XPU)岗位笔试机考题

华为海思COT和XPU部门IC前端,IC中端,IC后端岗位笔试真题分享

weixin_37584728的博客 3378

leetcode 814. Binary Tree Pruning | 814. 二叉树剪枝(Java)

题目 https://leetcode.com/problems/binary-tree-pruning/ 题解 思路很简单,看草稿: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val

寒泉 4万+

OpenClaw赚钱实录:从“养龙虾“到可持续变现的实践指南

OpenClaw已经开源3个月有余,作为深耕AI Agent领域的博主,我亲身探索了各种基于OpenClaw的赚钱方法,也踩过坑、付出了时间与精力。现在,我将这些真金白银砸出来的实战经验,毫无保留地总结成这个专栏,分享给不想从零开始摸索的你。博主在CSDN已有7万+粉丝和1100万+阅读量,但这却是我的第一个付费专栏。正因如此,我比任何人都更珍惜这个“第一次”。我会像一个手艺人打磨作品一样,死磕每一篇文章的质量,并持续根据订阅者的反馈优化内容。也许你的疑问就是当篇文章的优化点。

冯·诺依曼 2467

LeetCode 814. Binary Tree Pruning

原题链接在这里:https://leetcode.com/problems/binary-tree-pruning/ 题目: We are given the head noderootof a binary tree, where additionally every node's value is either a 0 or a 1. Return the same tree whe...

weixin_30952103的博客 79

灰度图像采集显示

灰度图像采集显示 在FPGA内部,采集到的视频数据线通过一个FIFO,将原本与25MHZ时钟同步的数据流转换到FPGA内部50MHZ的时钟。接着将这个数据再送入写DDR3缓存的异步FIFO中,这个FIFO中的数据一旦达到一定数量,就会被写入DDR3中。于此同时,使用另一异步FIFO缓存从DDR3读出的图像数据;LCD驱动模块不断地发出读图像数据请求给到这个FIFO,从中读取图像数据并送给VGA显示器进行实时图像的显示。 ...

小柚子的博客 492

YOLO26改进策略【YOLO和Mamba】| 2024 VM-UNet,高效的特征提取模块VSS block 二次创新提高精度

VM-UNet模型设计的出发点是解决现有CNN-based和模型在医学图像分割中的局限性,利用(SSMs)的优势,提出一种更有效的医学图像分割模型。具体来说,CNN-based模型在捕捉长距离信息方面存在不足,而模型由于自注意力机制的二次复杂度导致计算负担较重。SSMs模型如Mamba不仅在建模长距离依赖方面表现出色,还具有线性计算复杂度,这为VM-UNet的设计提供了理论基础。

Limiiiing的博客 244

[leetcode] 814. Binary Tree Pruning

Description We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1. Return the same tree where every subtree (of the given tree) not containing a ...

Learning from the mistakes 249

FPGA+CUYSB3014实现USB3.0功能

CYUSB3014是赛普拉斯在近几年推出的新一代USB3.0的外设控制器,可以解决USB2.0带宽限制,或者单独开发USB协议和驱动的难题。赛普拉斯将CYUSB3014简称为EZ-USB FX3,具有高度的灵活特性,开发人员只需要下载FX3的固件库,就能使用USB3.0的功能。目前在一些电子产品中,使用主控器加PHY芯片最流行的方式是用FPGA+FX3这种搭配来实现USB3.0接口。......

D_Katter的博客 7082

leetcode 81four. Binary Tree Pruning 二叉树剪枝

邹九的个人博客 217

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

[树] leetcode 814 Binary Tree Pruning

problem:https://leetcode.com/problems/binary-tree-pruning/ 一道非常简单的tree题目,根据题意模拟剔除不包含0的子树即可。 class Solution { public: bool dfs(TreeNode* p) { if (!p) return false; ...

ayzejx8547408的博客 111

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

【leetcode-Python】-Tree-814. Binary Tree Pruning

题目链接 https://leetcode.com/problems/binary-tree-pruning/solution/ 题目描述 给定一棵二叉树,树中每个节点的值要么为0,要么为1。从原二叉树中移除所有不包含1的子树后返回结果。 示例 输入:[1,0,1,0,0,0,1] 输出:[1,null,1,null,1] 解决思路 第一种方法是递归。递归函数的作用是判断以root为根节点的子树中是否含有1,如果没有1进行剪枝并返回False,如果含有1则返回True。递归结束条件是..

gulaixiangjuejue的博客 171

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

LeetCode 814. Binary Tree Pruning - 二叉树(Binary Tree)系列题29

给定一棵二叉树,要求把所有不含节点值为1的子树剪掉。这一道关于二叉树修剪的问题,实际生活中要修剪一棵树肯定是从叶子开始修剪。同样的,本题也应该从叶节点开始从底向上修剪。

hgq522的博客 282

Leetcode 814. Binary Tree Pruning(详细解析)

Leetcode 814. Binary Tree Pruning 题目链接: Binary Tree Pruning 难度:Medium 题目大意: 二叉树的节点要么是0要么是1,将二叉树中只含有0的子树去除掉。 思路: 递归,判断子树是否只含有0,如果是则去除掉。然后对左子树和右子树进行相同操作。 代码 自己写的 /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree

xue1236的博客 161

Leetcode 814. Binary Tree Pruning

dfs 要点是这一句: return node.val==1 or node.left or node.right 完整代码: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self....

weixin_30246221的博客 106

【面试题 & LeetCode 814】Binary Tree Pruning

题目描述 We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1. Return the same tree where every subtree (of the given tree) not containing a 1 has b...

iCode_girl的博客 188

LeetCode C++ 814. Binary Tree Pruning【Tree/Recursion】中等

We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. Return the same tree where every subtree (of the given tree) not containing a 1 has been removed. (Recall that the subtree of a node X is X,

memcpy0的博客 369

LeetCode - Medium - 814. Binary Tree Pruning

Topic Tree Description https://leetcode.com/problems/binary-tree-pruning/ Given the root of a binary tree, return the same tree where every subtree (of the given tree) not containing a 1 has been removed. A subtree of a node node is node plus every node

KISS 275

leetcode 814. 二叉树剪枝(Binary Tree Pruning)

给定二叉树根结点 root ,此外树的每个结点的值要么是 0,要么是 1。 返回移除了所有不包含 1 的子树的原二叉树。 ( 节点 X 的子树为 X 本身,以及所有 X 的后代。) 示例1: 输入: [1,null,0,0,1] 输出: [1,null,0,null,1] 解释: 只有红色节点满足条件“所有不包含 1 的子树”。 右图为返回的答案。 示例2: 输入: [1,0...

一叶之修的博客 349

[Leetcode] 814. Binary Tree Pruning 解题报告

题目: We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. Return the same tree where every subtree (of the given tree) not containing

魔豆(Magicbean)的博客 462

[LeetCode] 814. Binary Tree Pruning

Problem We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. Return the same tree where every subtree (of the giv...

weixin_34200628的博客 83

LeetCode 3884.双端字符匹配:半次遍历

LeetCode 3884.双端字符匹配:半次遍历 给你一个长度为 n 的字符串 s,其中只包含小写英文字母。 返回最小的下标 i,使得 s[i] == s[n - i - 1]。 如果不存在这样的下标,返回 -1。

<font color="Pink">Tisfy的博客</font> 169
上一篇: leetcode 701. Insert into a Binary Search Tree(二叉搜索树插入节点)
下一篇: leetcode 863. All Nodes Distance K in Binary Tree(二叉树中距离K的节点)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址