leetcode 1110. 删点成林
题目描述:
给出二叉树的根节点 root,树上每个节点都有一个不同的值。
如果节点值在 to_delete 中出现,我们就把该节点从树上删去,最后得到一个森林(一些不相交的树构成的集合)。
返回森林中的每棵树。你可以按任意顺序组织答案。
示例:
输入:root = [1,2,3,4,5,6,7], to_delete = [3,5]
输出:[[1,2,null,4],[6],[7]]
提示:
树中的节点数最大为 1000。
每个节点都有一个介于 1 到 1000 之间的值,且各不相同。
to_delete.length <= 1000
to_delete 包含一些从 1 到 1000、各不相同的值。
思路分析:
涉及树,利用递归求解。对于每一棵树,若其在待删除的结点数组中,且其父节点不在当前森林中,则添加这棵树到当前森林。其中在递归过程可以更新每棵树的左右子树,根据其左右子树的根结点是否在待删除结点数组中进行判断,若在,则当前的树的对应左右子树置为空。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
unordered_set<int> delete_node;
vector<TreeNode*>res;
TreeNode* preorder(TreeNode* root, bool n_root)
{
if(root == nullptr)
return nullptr;
bool is_delete = delete_node.count(root->val)>;
if(!is_delete && n_root)
res.push_back(root);
root->left = preorder(root->left, is_delete);
root->right = preorder(root->right, is_delete);
return is_delete ? nullptr:root;
}
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
if(root==nullptr)
return res;
delete_node = unordered_set<int>(to_delete.begin(), to_delete.end());
preorder(root, true);
return res;
}
};
leetcode 1110. 删点成林的更多相关文章
- LeetCode 1110. Delete Nodes And Return Forest
原题链接在这里:https://leetcode.com/problems/delete-nodes-and-return-forest/ 题目: Given the root of a binary ...
- Leetcode题解 - DFS部分题目代码+思路(756、1034、1110、491、721、988)
756. 金字塔转换矩阵 """ 学到的新知识: from collections import defaultditc可以帮我们初始化字典,不至于取到某个不存在的值的时 ...
- LeetCode刷题总结-树篇(下)
本文讲解有关树的习题中子树问题和新概念定义问题,也是有关树习题的最后一篇总结.前两篇请参考: LeetCode刷题总结-树篇(上) LeetCode刷题总结-树篇(中) 本文共收录9道题,7道中等题, ...
- [leetcode]380. Insert Delete GetRandom O(1)设计数据结构,实现存,删,随机取的时间复杂度为O(1)
题目: Design a data structure that supports all following operations in average O(1) time.1.insert(val ...
- 【LeetCode】1110. Delete Nodes And Return Forest 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- [leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 【leetcode】1110. Delete Nodes And Return Forest
题目如下: Given the root of a binary tree, each node in the tree has a distinct value. After deleting al ...
- leetcode 学习心得 (2) (301~516)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- docker-machine命令安装
$ base=https://github.com/docker/machine/releases/download/v0.16.0 && curl -L $base/docker-m ...
- 2019-11-29-win7-无法启动-WPF-程序-D3Dcompiler_47.dll-丢失
原文:2019-11-29-win7-无法启动-WPF-程序-D3Dcompiler_47.dll-丢失 title author date CreateTime categories win7 无法 ...
- WPF 精修篇 Winform 嵌入WPF控件
原文:WPF 精修篇 Winform 嵌入WPF控件 首先 创建WPF控件库 这样就有了一个WPF界面 在wpf中增加界面等 在winform中增加WPFDLL 重新生成解决方案 在左侧工具栏 出现W ...
- mosquitto 常用命令
原文:https://www.cnblogs.com/smartlife/articles/10182136.html 常用命令 订阅主题 mosquitto_sub -h 192.168.0.1 - ...
- form.submit()提交后返回数据的处理
form.submit()发送请求一般是单向的,如果需要取返回的数据,一般会发送ajax请求,但是如果form中有附件呢?(以后有时间给大家分享ajax上传附件的功能),确实需要返回数据来知道该功能是 ...
- C# Newtonsoft.Json JsonSerializerSettings配置序列化操作
https://blog.csdn.net/u011127019/article/details/72801033
- nginx fastcgi模块ngx_http_fastcgi_module详细解析、使用手册、完整翻译
ngx_http_fastcgi_module 模块允许将请求传递给 FastCGI 服务器. 示例配置 location / { fastcgi_pass localhost:9000; fastc ...
- saltstack的简单搭建
环境; centos 7 192.168.10.10 master centos 7 192.168.10.129 minion 1.为了方便关闭防火墙 [root@local ...
- wait waitpid
定义 pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); 暂时停止进程的执行,直到有信号来到或子进 ...
- 使用 Python 生成二维码
在“一带一路”国际合作高峰论坛举行期间, 20 国青年投票选出中国的“新四大发明”:高铁.扫码支付.共享单车和网购.其中扫码支付指手机通过扫描二维码跳转到支付页面,再进行付款.这种新的支付方式,造就二 ...