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 ...
随机推荐
- 关于thinkphp3.2.3集成phpmailer
关于thinkphp3.2.3集成phpmailer 1 我用的是phpmailer5.1的版本 先把文件解压缩放到这个位置 2 封装到函数里面 function email($email,$tit ...
- Flutter实体与JSON解析的一种方法
vs code作为编辑器 1. 首先,json对象与字符串的转换是使用json.encode和json.decode的,需要导入import 'dart:convert'; 这里主要的自然不是这个,而 ...
- HTTP STATUS 400 – BAD REQUEST ,SPRINGMVC错误
400大多为前台传的数据于后台接受数据不符合,注意Date数据类型最容易错. 然后需要调用实体类的空参构造方法,,注意创建了有参构造方法后,创建一个空参构造方法.
- Spring事务源码解析(二)获取增强
在上一篇文章@EnableTransactionManagement注解解析中,我们搭建了源码阅读的环境,以及解析了开启Spring事务功能的注解@EnableTransactionManagemen ...
- vue+element表单校验功能
要实现这个功能其实并不难,element组件直接用就可以, 但是我在使用过程中碰到了几个坑,就记录下来,分享给大家,避免落坑,话不多说,直接上过程...... 表单校验功能: 实现这个功能,总共分 ...
- CRM product model的用法
User scenario An example from sap help For a car, the interior, the engine capacity, and the exterio ...
- vue开发环境配置
一.开发工具 Visual Studio Code 二.环境搭建 vue推荐开发环境: Node.js: javascript运行环境(runtime),不同系统直接运行各种编程语言 npm: Nod ...
- k8s dashboard 解决secret自建证书导致浏览器访问限制
解决参考: https://www.jianshu.com/p/c6d560d12d50 熟悉dashboard yaml文件所创建的资源 wget https://raw.githubuserc ...
- django-配置404页面
setting.py文件配置 # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False ALLOW ...
- Alipay 支付类
本版本参考网友 <?php namespace App\Tools; class Alipay { //应用ID,您的APPID. private $appID = '111'; //商户私钥 ...