当前位置:网站首页>Force deduction problem: DFS recursion to solve binary tree pruning
Force deduction problem: DFS recursion to solve binary tree pruning
2022-07-22 17:11:00 【W_ oilpicture】
Preface
Life is a journey , I am also a pedestrian .
/** * 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:
bool dfs(TreeNode* root){
if(!root) return false; // Empty node means no 1, return false
bool left = dfs(root->left); // Judge whether there is 1
bool right = dfs(root->right); // Judge whether there is 1
if(!left) root->left = NULL; // There is nothing on the left 1, Delete the left subtree
if(!right) root->right = NULL; // There is nothing on the right 1, Then delete the right subtree
if(root->val != 1 && !left && !right)
return false; // Not on the left 1 And there is no on the right 1 And I'm not 1, Should delete ( return false)
return true;
}
TreeNode* pruneTree(TreeNode* root) {
bool valid = dfs(root);
if(!valid)
root = nullptr; // If there is no whole tree 1 Null node is returned
return root;
}
};
/** * 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:
bool seek(TreeNode* root)
{
if(root == NULL)
return true;
if(!seek(root->left))
root->left = NULL; //
if(!seek(root->right))
root->right = NULL;
return root->val || root->left || root->right;// If there are children, it is not empty ( Children contain 1), Or this node is 1 Cannot delete this node
}
TreeNode* pruneTree(TreeNode* root) {
if(seek(root))
return root;
return NULL;
}
};
边栏推荐
- npm私服发包及使用
- [redis] redis high availability deployment scheme in distributed scenarios
- Zen administrator forgets password and retrieves password
- Hande x Jiuli special materials | work together to create a collaborative office portal and help it internal standardized management
- 从0到1建设智能灰度数据体系:以vivo游戏中心为例
- [red team] att & CK - browser extension for persistence
- 【vs】 试图加载格式不正确的程序
- 【红队】ATT&CK - 浏览器扩展实现持久化
- tf.set_random_seed()
- Hande enterprise PAAS platform hzero will soon be heavily open source!
猜你喜欢
pytorch优化器: optim.SGD && optimizer.zero_grad()
Nvidia 硬件架构
Ffmpeg-rk3399 ffplay learning analysis
工作流引擎在vivo营销自动化中的应用实践 | 引擎篇03
汉得x久立特材|携手打造协同办公门户,助力IT内部规范管理
跨域问题(CORS)详细说明和解决
tf.reduce_sum()
Ora-16525 DG broker not available
禅道管理员忘记密码找回密码
[redis] redis high availability deployment scheme in distributed scenarios
随机推荐
Opencv supports H264 video coding
1143. 最长公共子序列
UE4 combines the objects made by the brush into a whole
Hande enterprise PAAS platform hzero will soon be heavily open source!
tf.compat
[redis] redis high availability deployment scheme in distributed scenarios
tf.get_ default_ graph
tf.random_normal_initializer
SAP WPER(POS接口监控器)IDCO过账凭证ALV报表
Four main steps of web application penetration testing
MySQL foundation +mysql cluster review
JSON_EXTRACT返回不正确问题
1840. The highest building height is greedy
【MySQL】sql调优实战教学
工作流引擎在vivo营销自动化中的应用实践 | 引擎篇03
Concis component library | dark pattern design
[MySQL] SQL tuning practice teaching
汉得数字平台体系及试用知多少?
Cartopy绘图入门指南
汉得企业级PaaS平台 HZERO 发布 1.5.0.RELEASE 版本