当前位置:网站首页>LeetCode·每日一题·814.二叉树剪枝·递归
LeetCode·每日一题·814.二叉树剪枝·递归
2022-07-21 12:56:00 【小迅想变强】
链接:https://leetcode.cn/problems/binary-tree-pruning/solution/c-by-xun-ge-v-9zt4/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
示例
思路
解题思路
遇到树相关问题递归基本上都可以解决,因为树本身就可以用递归构造出来
关于本题需要移除了所有不包含 1 的子树的原二叉树。对于整个树而言,我们需要移除节点不为1的子树节点,将此问题划分为若干个子问题->即为每一个节点,移除不为1的左右子节点
那么每一步递归的子问题
- 都是移除当前节点左右子节点不为1的节点
具体实现
官方代码比我自己写的好很多
本人思路是递归遍历每一个节点,同时采用深度优先搜索原则,先遍历子节点,因为每一个节点都需要考虑同一个子问题移除当前节点左右子节点不为1的节点,所以需要对每一个节点都进行判断,判断左节点和右节点是否为0,为0移除对应子节点
代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int dfs(struct TreeNode * root)
{
if(root == NULL)
{
return 0;
}
int left = 0;//记录左子节点值
left = dfs(root->left);//遍历左子节点,当左子节点一直存在会优先遍历左子节点,一直到左节点的叶子结点
if(left == 0)//为0,移除
{
free(root->left);
root->left = NULL;
}
int right = 0;//记录右子节点
right = dfs(root->right);//遍历右子节点
if(right == 0)
{
free(root->right);
root->right = NULL;
}
int n = (left > right ? left : right);//返回左子节点和右子节点中是否存在1
return n > root->val ? n : root->val;
}
/*
*struct TreeNode* pruneTree(struct TreeNode* root)
struct TreeNode* pruneTree:移除了所有不包含 1 的子树的原二叉树
struct TreeNode* root:头结点
返回值:移除后的新二叉树头结点
*/
struct TreeNode* pruneTree(struct TreeNode* root){
int m = dfs(root);//头节点也有可能为0,也移除
if(m == 0)
{
return NULL;
}
return root;
}
时间空间复杂度
边栏推荐
猜你喜欢
打假AI无主灯,欧瑞博、艾拉物联、绿米Aqara谁是玩真的?
Apple lost 340million Yuan due to bad keyboard. SpaceX received the order. Webb's successor, meta, sued meta. Today, more new things are here
振华风光半导体通过注册:年营收5亿 中国电子是实控人
【微信小程序】WXSS和全局、页面配置入门砖
[chestnut sugar GIS] ArcMap -- how to make text four Zhi of surface data
PL-Marker(ACL 2022)——信息抽取(NER+RE)新SOTA,论文浅析与代码浏览
Ningmeng film and television passed the hearing: Tencent and mango cultural innovation are shareholders with an annual revenue of 1.2 billion
(字符串+内存)函数的介绍
Mipi based high performance imaging system
深度之眼(十五)——导数
随机推荐
归并排序解决逆序对的数量问题
[BSP video tutorial] BSP video tutorial No. 20: play with serial port special topics, Hal library, ll library and register implementation methods, as well as learning several key sequence diagrams in
Lianying medical passed the registration: it plans to raise 12.5 billion yuan, and Xue min controls 32% of the equity
你离「TDengine 开发者大会」只差一条 SQL 语句!
[Suzhou University] information sharing of postgraduate entrance examination and re examination
Command line use of eslint
Anfulai embedded weekly report no. 274: 2022.07.11--2022.07.17
阿里云ECS手动挂载磁盘
Merge sort solves the quantity problem of reverse order pairs
PG 的数组下标是从1开始的
If there is out in PG function, returns setof record as is ignored$$
Mipi based high performance imaging system
Apple lost 340million Yuan due to bad keyboard. SpaceX received the order. Webb's successor, meta, sued meta. Today, more new things are here
【微信小程序】WXSS和全局、页面配置入门砖
Ningmeng film and television passed the hearing: Tencent and mango cultural innovation are shareholders with an annual revenue of 1.2 billion
Academia vs industry: can deep learning break the ceiling of video coding and decoding
归并排序思路及例题
人人都能用的多语种大模型来了!支持59种语言,参数1760亿,1000名科学家联合发起...
基于SqlSugar的开发框架循序渐进介绍(12)-- 拆分页面模块内容为组件,实现分而治之的处理
Subvert cognition! There are as many as 13 methods to guarantee research?