当前位置:网站首页>[C language brush leetcode] 814. Binary tree pruning (m)
[C language brush leetcode] 814. Binary tree pruning (m)
2022-07-26 02:00:00 【kinbo88】
【
The root node of your binary tree root , In addition, the value of each node of the tree is either 0 , Or 1 .
Return to remove all excluded 1 The original binary tree of the subtree .
node node The subtree of is node Plus all itself node The offspring of .
Example 1:
Input :root = [1,null,0,0,1]
Output :[1,null,0,null,1]
explain :
Only the red nodes meet the conditions “ All do not contain 1 The subtree of ”. On the right is the answer .
Example 2:
Input :root = [1,0,1,0,0,0,1]
Output :[1,null,1,null,1]
Example 3:
Input :root = [1,1,0,1,1,0,1,0]
Output :[1,1,0,1,1,null,1]
Tips :
The number of nodes in the tree is in the range [1, 200] Inside
Node.val by 0 or 1
source : Power button (LeetCode)
link :https://leetcode.cn/problems/binary-tree-pruning
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
】
Say it's a medium question , It looks like a simple problem , It is estimated that we should pay attention to some details .
int dfs(struct TreeNode* root)
{
if (root == NULL) { // achieve NULL, Start back to
return 0;
}
int lf = dfs(root->left);
int rf = dfs(root->right);
if (lf == 0) {
root->left = NULL;
}
if (rf == 0) {
root->right = NULL;
}
if (lf == 0 && rf == 0 && root->val == 0) {// If the left and right nodes are empty and they are equal to 0, return 0, It means it will be deleted
return 0;
}
return 1;
}
struct TreeNode* pruneTree(struct TreeNode* root){
int rf = dfs(root); // Start a deep search for , And return whether the current node is a leaf node
if (rf == 0 && root->val == 0) {
return NULL;
}
return root;
}边栏推荐
- opengauss如何手工安装(非OM方式)
- Navica工具把远程MySQL导入到本地MySQL数据库
- MPLS knowledge points
- 保护系统日志服务器和设备
- leetcode/只出现一次的数字
- There is no setter method in grpc list under flutter. How to use related attributes
- How to install opengauss manually (non om mode)
- Remember a laravel problem script @php artist package:discover handling the post autoload dump event returned with
- What is the difference between for... In... And for... Of
- vite 本地运行首次进入页面加载慢问题
猜你喜欢

Navica工具把远程MySQL导入到本地MySQL数据库

还在用==0 null equal 判断空值吗,对isEmpty 和 isBlank有多少了解呢

SQL手工盲注、报错注入

一款可插拔的AM335X工控模块板载wifi模块

What is the difference between for... In... And for... Of

【2021】【论文笔记】6G技术愿景——OTFS调制技术

pt-onnx-ncnn转换的问题记录(接yolov5训练)

Worthington nuclease and Micrococcus related research and determination scheme

(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
![Niuke - bm39 serialized binary tree [hard]](/img/c4/f14fe8488bbf28689fa3f02cdf4dae.png)
Niuke - bm39 serialized binary tree [hard]
随机推荐
How to do Taobao live broadcast and how to do the anchor to drain and sell products
Make and makefile summary II
Alibaba cloud redis development specification
How to modify Oracle functions?
Composition API的优势
【2019】【论文笔记】基于超材料可调谐THz宽频吸收——
How idea can quickly delete recently opened projects
# Dest0g3 520迎新赛(更新中)
win下搭建嵌入式开发环境及frp穿透
【2021】【论文笔记】6G技术愿景——OTFS调制技术
PHP Alipay transfer to Alipay account
A MCU event driven C framework
【2021】【论文笔记】红外及THz下的细胞膜生物效应——效应是现象,作用是机理——THz对医学的好处
NFT access tool premint was hacked and lost more than 370000 US dollars
Three modes of CPU
Build embedded development environment and FRP penetration under win
mysql 事务隔离级别
6 + 1 skills of Software Test Engineer
Redis cluster construction (based on 6.x)
还在用==0 null equal 判断空值吗,对isEmpty 和 isBlank有多少了解呢