当前位置:网站首页>LeetCode 0814. 二叉树剪枝
LeetCode 0814. 二叉树剪枝
2022-07-22 02:11:00 【Tisfy】
【LetMeFly】814.二叉树剪枝
力扣题目链接:https://leetcode.cn/problems/binary-tree-pruning/
给你二叉树的根结点 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
方法一:DFS
递归,返回结果的同时进行修剪。
因为涉及到一个节点的所有子树,因此很适合深搜DFS。
构建一个函数isZero
,用来判断一个节点是否是某个全0子树
的根。同时,如果这个节点的左子树是全0子树,就剪掉这个节点的左子树;右子树同理。
/* 判断此节点的 左/右 子树是否为全0二叉树。若是,则移除对应子树 */
bool isZero(TreeNode* root) {
bool is0 = true; // 这个节点是否为全0子树的根
if (root->val) // 如果这个节点的值为1
is0 = false; // 直接排除全0子树
// 判断左子树是否为全0子树。如果是,就修剪之
if (root->left) {
// 前提是左子树不空
if (isZero(root->left)) {
// 左子树是全0子树
root->left = nullptr; // 左子树设置为空(这样就修剪掉了)
}
else {
// 左子树不是全0子树
is0 = false; // 那么“父树”更不是全0树
}
}
// 右子树同理
if (root->right) {
if (isZero(root->right)) {
root->right = nullptr;
}
else {
is0 = false;
}
}
return is0;
}
- 时间复杂度 O ( N ) O(N) O(N),其中 N N N是节点的个数
- 空间复杂度 O ( N ) O(N) O(N),空间复杂度主要来自递归
AC代码
C++
class Solution {
private:
/* 判断此节点的 左/右 子树是否为全0二叉树。若是,则移除对应子树 */
bool isZero(TreeNode* root) {
bool is0 = true;
if (root->val)
is0 = false;
if (root->left) {
if (isZero(root->left)) {
root->left = nullptr;
}
else {
is0 = false;
}
}
if (root->right) {
if (isZero(root->right)) {
root->right = nullptr;
}
else {
is0 = false;
}
}
return is0;
}
public:
TreeNode* pruneTree(TreeNode* root) {
if (isZero(root))
root = nullptr;
return root;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125918905
边栏推荐
- Redis主从复制
- 华为云从入门到实战 | AI云开发ModelArts入门与WAF应用与部署
- Distributed link tracking skywalking practice
- Waiting insurance compliance 2022 series | what should you know about waiting insurance this year?
- vlc报错——“live555 error: no data received in 10s, aborting”
- ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or
- Values swxxdp calculation in Res
- Pdf to image and content reading
- OUU益生菌精耕胃肠健康,获奖天猫国际微生态创新大会
- 双人成行本地安装&X360ce模拟手柄教程&xpadder手柄模拟键盘鼠标
猜你喜欢
华为云从入门到实战 | AI云开发ModelArts入门与WAF应用与部署
Wafer thickness measurement
JDBC编程
Waiting insurance compliance 2022 series | what should you know about waiting insurance this year?
调试VBS visual studio
Introduction to microservices
还在问用什么来做接口测试?万能Jmeter打造性能测试数据平台
Architecture design scheme (continuously updating ing)
记线上双写失败日志mysql错误排查原因
Redis缓存穿透和雪崩
随机推荐
Redis缓存穿透和雪崩
华为云从入门到实战 | AI云开发ModelArts入门与WAF应用与部署
A simple inventory management system source code
Values swxxdp calculation in Res
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or
IO 和NIO的区别
How to use first-hand data visualization to win the favor of the boss and grasp the key points of data visualization
How Linux queries Oracle error logs
如何用一手数据可视化获得老板的青睐,抓住数据可视化关键点
Graffiti Wi Fi & ble SoC development slide strip (6) -- slide strip function demonstration
【C语言-程序编译】一行行代码究竟是怎么一步步到可执行程序的?
[Huawei machine test questions] maximum number of components [2022 Q3 | 100 points]
OpenSSL self signed certificate issuance script -- the road to building a dream
Redis使用Jedis操作
DM8:查询达梦数据库数据文件使用大小限制
微信小程序反编译
电流探头应该如何选择
[independent station operation] Shopify sellers: how to improve the store experience? Two moves are done!
JDBC programming
Succès de la construction du cluster expérimental tdengine