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 ...
随机推荐
- 浅谈Semaphore类-示例
Semaphore类有两个重要方法 1.semaphore.acquire(); 请求一个信号量,这时候信号量个数-1,当减少到0的时候,下一次acquire不会再执行,只有当执行一个release( ...
- 基于串口的SD_card系统
概述 基于串口的SD_card系统1, 扫描文件:2, 新建文件:3, 删除文件:4, 写入文件:5, 读取文件. 整个文件系统的串口通信方式都是ASC通信方式. 文件系统分为简单实用方式和专业使用方 ...
- mysql 查询数据库表信息,字段信息
#======================================================================= #查询表信息 select table_name, t ...
- JAVA操作InfluxDB的一个Demo
一.基础连接类 package com.test.repository.utils; import com.dcits.domain.entry.bo.common.InfluxDbRow; impo ...
- 使用electron在mac升级签名后进行升级出现“QRLUpdaterErrorDomain”的错误
现在在开发electron客户端,windows签名使用了签名狗的方式进行签名(小坑:签名狗只能对打包完的exe进行签名,而electron-builder需要在打包的时候将证书配置进去,所以需要导出 ...
- ES6 入门系列 (三) 尾递归
递归我们不陌生, 那什么是尾递归呢? 为什么要用尾递归呢? 尾递归怎么用呢? 带着这三个问题我们来了解它, 我们知道递归非常耗费内存,一不小心就会发生‘栈溢出’, 相信你一定遇到过这个错误: stac ...
- Cloud Alert 实现告警智能降噪,成功规避告警风暴
# 前言 睿象云前段时间发表了一篇[< Zabbix 实现电话.邮件.微信告警通知的实践分享>](https://www.toutiao.com/i6734876723126469127/ ...
- 给easyui datebox扩展一个清空按钮,无侵入
/** * 给时间框控件扩展一个清除的按钮 */ $.fn.datebox.defaults.cleanText = '清空'; (function ($) { var buttons = $.ext ...
- 遇到的一个Buffer too small问题
在ROI中输出图像时遇到 经调试后发现是driver.Create时设置的波段数大于实际写入的波段数导致的 这里xImgIn.m_nBands有204,但实际写入的数据的bands只有3,修改时忘了修 ...
- Model 中的Meta类选项
通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...