当前位置:网站首页>[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;
}边栏推荐
- 【独立站建设】shopify卖家:学会这几点,网上商店销量翻倍!
- 劳驾问一下各位老师 oracle 到pg cdc oracle 那边字段大写 pg 这边小写 同
- AUTOCAD——计算面积的方法
- I.MX6UL核心模块使用连载-CAN、蜂鸣器测试 (十一)
- How to display numbers / English time in Excel
- 【2021】【论文笔记】红外及THz下的细胞膜生物效应——效应是现象,作用是机理——THz对医学的好处
- Jupiter notebook reported an error: notebook validation failed: non unique cell ID '2a4xx6' detected
- HTC手机官解、S-ON/S-OFF与超级CID的关系
- The import and Export button of Damon database table is gray, and the DMP file cannot be imported
- There is no setter method in grpc list under flutter. How to use related attributes
猜你喜欢

Protect syslog servers and devices

Digital transformation behind the reshaping growth of catering chain stores

【Verilog数字系统设计(夏宇闻)4-----Verilog语法的基本概念2】

C# 迭代器的实现

Why does the debugger display the wrong function

What is cross site scripting (XSS)?
![[independent station construction] Shopify seller: learn these points and double the sales volume of online stores!](/img/52/8c1520db38ffa8927e975b6f244a65.png)
[independent station construction] Shopify seller: learn these points and double the sales volume of online stores!

i.MX6ULL SNVS电源域GPIO状态保持验证

Overview of database stress testing methods

NFT market also began to diversify
随机推荐
Niuke - bm39 serialized binary tree [hard]
Protect syslog servers and devices
【2020】【论文笔记】磁控溅射法生长Bi2Te3/CoFeB双层异质结——
Study notes: original code, inverse code, complement code
Redis cluster construction (based on 6.x)
flink sql 如何配置打印insert实参日志呢
Shell summary (1)
How does Flink SQL configure to print the insert parameter log
MySQL locking table problem
[in simple terms, play with FPGA learning 11 --- testbench writing skills 1]
[Android development IOS series] Language: swift vs kotlin
NFT access tool premint was hacked and lost more than 370000 US dollars
FFT is used to estimate the image resampling factor after interpolation
Ti AM335X工控模块矩阵键盘电路的设计与驱动移植
D - Dire Wolf (interval DP)
登堂入室soc之arm汇编基础
6 + 1 skills of Software Test Engineer
Software group verification
IDEA如何快速删除最近打开的项目
Summary after reading "poor dad and rich dad"