当前位置:网站首页>Leetcode 814 binary tree pruning [dfs] the leetcode road of heroding
Leetcode 814 binary tree pruning [dfs] the leetcode road of heroding
2022-07-22 06:40:00 【HERODING23】
Their thinking :
A very standard dfs subject , Keep going down to the bottom of the original node , When encountering an empty node, it returns null , Leaf node is encountered and the value is 0 Return null of , Logically speaking, in the process of pruning , If the current node is 0, If the left and right child nodes do not exist or the values are 0 Just pruning , But in dfs In the process , The value is 0 The leaf node of has returned nullptr 了 , That is, every time you prune, it is the leaf node ( It is not a leaf, but also becomes a leaf because of the pruning of the left and right child nodes ), The code is as follows :
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(root == nullptr) return nullptr;
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if(root->left == nullptr && root->right == nullptr && root->val == 0) {
return nullptr;
}
return root;
}
};
边栏推荐
猜你喜欢
jetbrans rider 格式化代码时,{}不换行
Seaborn的使用
抽象类与接口
β- Preparation of cyclodextrin derivative grafted hydroxypropyl chitosan hydrogel / carboxyl modified chitosan supported cyclodextrin hydrogel microspheres
同城订单同城送,爆单依旧得心应手!
Animation, and basic use of animation
Dom4j parsing XML (detailed)
攻防演练进行时|知道创宇赵伟:让安全能力长在云上
运营商AI机遇:以大模型拓展全新赛道
神仙测试软件藏不住了|属于中国人的“LabVIEW”你知道吗?
随机推荐
UE4 学习笔记 CheckBox替代button + image
LeetCode刷题:对称二叉树与二叉树的最大深度
壳聚糖/葡聚糖/纳米羟基磷灰石复合水凝胶/鱼明胶-半乳糖壳聚糖水凝胶肝支架的制备
聚醚/聚丙烯酰胺-竣甲基/聚丙烯酰胺/粒状聚N-异丙基丙烯酰胺壳聚糖水凝胶的制备方法
Nisp-2 information security technology series blog directory
One click free download of foreign documents
Floating profile and floating features
Using LINGO to solve simple linear programming problems
Preparation method of polyether / polyacrylamide monomethyl / Polyacrylamide / granular poly (N-isopropylacrylamide) chitosan hydrogel
idea 改变字体大小
β-环糊精衍生物接枝羟丙基壳聚糖水凝胶/羧基改性壳聚糖固载环糊精水凝胶微球的制备
OSPF实验演示(Huawei路由器设备配置)
吉时利Keithley软件2600系列2611B|2612B|2614B|2634B NS-SourceMeter源表软件
unity GetAxis(string axisName); axisName 值怎么来的
DOM event flow of DOM series
Length unit, color unit, text style and font style
6.1 malicious code prevention
Episode 2 best B tutorial of VMware virtual machine installation (13 days)
DOM Series event object
神仙测试软件藏不住了|属于中国人的“LabVIEW”你知道吗?