当前位置:网站首页>LeetCode.814. 二叉树剪枝____DFS
LeetCode.814. 二叉树剪枝____DFS
2022-07-27 09:42:00 【向光.】
814. 二叉树剪枝
给你二叉树的根结点 root ,此外树的每个结点的值要么是 0 ,要么是 1 。
返回移除了所有不包含 1 的子树的原二叉树。
节点 node 的子树为 node 本身加上所有 node 的后代。
示例 1:
输入:root = [1,null,0,0,1]
输出:[1,null,0,null,1]
解释:
只有红色节点满足条件“所有不包含 1 的子树”。 右图为返回的答案。
示例 2:
输入:root = [1,0,1,0,0,0,1]
输出:[1,null,1,null,1]
示例 3:
输入:root = [1,1,0,1,1,0,1,0]
输出:[1,1,0,1,1,null,1]
提示:
- 树中节点的数目在范围 [1, 200] 内
- Node.val 为 0 或 1
Solution:
- 我们直接深搜此二叉树,当左节点右节点均为null且此节点对应val为0时,将此节点改为null即可;
Code:
/** * 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 TreeNode pruneTree(TreeNode root) {
if(root.left != null)
root.left = pruneTree(root.left);
if(root.right != null)
root.right = pruneTree(root.right);
if(root.left == null && root.right == null && root.val == 0){
return null;
}
return root;
}
}
边栏推荐
- 如何在树莓派上安装cpolar内网穿透
- Google Earth Engine APP——打印点的坐标到控制台上和map上,设置样式并更新
- 七月集训(第08天) —— 前缀和
- Final examination paper of engineering materials
- 安装了HAL库如何恢复原来的版本
- Exercises --- quick arrangement, merging, floating point number dichotomy
- July training (day 20) - binary search tree
- 华为交换机双上行组网Smart-link配置指南
- Understand chisel language. 25. Advanced input signal processing of chisel (I) -- asynchronous input and de jitter
- Understand chisel language. 24. Chisel sequential circuit (IV) -- detailed explanation of chisel memory
猜你喜欢

It's great to write code for 32 inch curved screen display! Send another one!

Esp8266 Arduino programming example - interrupt

Understand chisel language. 23. Chisel sequential circuit (III) -- detailed explanation of chisel shift register

省应急管理厅:广州可争取推广幼儿应急安全宣教经验
![WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]](/img/94/92ad89751e746a18edf80296db9188.png)
WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]

刷题《剑指Offer》day03

Lua函数嵌套调用

Understand chisel language. 27. Chisel advanced finite state machine (I) -- basic finite state machine (Moore machine)

快应用自定义进度条

wordpress禁止指定用户名登录或注册插件【v1.0】
随机推荐
食品安全 | 垃圾食品越吃越想吃?这份常见食品热量表请收好
七月集训(第10天) —— 位运算
ESP8266-Arduino编程实例-PWM
July training (day 20) - binary search tree
NPM common commands
Esp8266 Arduino programming example ADC
七月集训(第09天) —— 二分查找
Nacos做注册中心使用
刷题《剑指Offer》day03
给自己写一个年终总结,新年快乐!
Understand chisel language. 23. Chisel sequential circuit (III) -- detailed explanation of chisel shift register
[cloud native • Devops] master the container management tool rancher
July training (day 19) - binary tree
Understand chisel language. 25. Advanced input signal processing of chisel (I) -- asynchronous input and de jitter
圆环工件毛刺(凸起)缺口(凹陷)检测案例
35-Spark Streaming反压机制、Spark的数据倾斜的解决和Kylin的简单介绍
吃透Chisel语言.27.Chisel进阶之有限状态机(一)——基本有限状态机(Moore机)
Nccl collective communication --collective operations
July training (day 06) - sliding window
七月集训(第06天) —— 滑动窗口


