【LeetCode】1110. Delete Nodes And Return Forest 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/delete-nodes-and-return-forest/
题目描述
Given the root of a binary tree, each node in the tree has a distinct value.
After deleting all nodes with a value in to_delete
, we are left with a forest (a disjoint union of trees).
Return the roots of the trees in the remaining forest. You may return the result in any order.
Example 1:
Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
Output: [[1,2,null,4],[6],[7]]
Constraints:
- The number of nodes in the given tree is at most 1000.
- Each node has a distinct value between 1 and 1000.
to_delete
.length <= 1000to_delete
contains distinct values between 1 and 1000.
题目大意
删除一棵二叉树中的所有值出现在to_delete中的节点。
解题方法
递归
参考了lee215大神的答案。看到二叉树的题就想到递归呀!
一个节点被删除时有以下几个情况:
- 如果该节点是根节点,形成左右两个子树,此时递归左右子树。
- 如果该节点不是根节点,那么需要修改其父节点指向自己的指针为空,并且递归左右子树。
一个节点一旦被删除,那么其左右孩子就是新的树的根节点。
如果一个节点是根节点,并且不被删除的情况下,才会放入结果中。
C++代码如下:
/**
* 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:
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
vector<TreeNode*> res;
helper(root, true, res, to_delete);
return res;
}
void helper(TreeNode*& node, bool isRoot, vector<TreeNode*>& res, vector<int>& to_delete) {
if (!node) return;
bool isDel = delCurNode(node, to_delete);
helper(node->left, isDel, res, to_delete);
helper(node->right, isDel, res, to_delete);
if (isRoot && !isDel) {
res.push_back(node);
}
if (!isRoot && isDel) {
node = nullptr;
}
}
bool delCurNode(TreeNode* root, vector<int>& to_delete) {
for (int val : to_delete) {
if (root->val == val) {
return true;
}
}
return false;
}
};
参考资料:https://leetcode.com/problems/delete-nodes-and-return-forest/discuss/328853/JavaC%2B%2BPython-Recursion-Solution
日期
2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题
【LeetCode】1110. Delete Nodes And Return Forest 解题报告 (C++)的更多相关文章
- LeetCode 1110. Delete Nodes And Return Forest
原题链接在这里:https://leetcode.com/problems/delete-nodes-and-return-forest/ 题目: Given the root of a binary ...
- 【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 944 Delete Columns to Make Sorted 解题报告
题目要求 We are given an array A of N lowercase letter strings, all of the same length. Now, we may choo ...
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- Xshell初步设置
目录 双击复制,右击粘贴 双击复制全路径 复制窗口:双击窗口 编码:设置utf-8 外观设置: 窗口化文件传输 vim中使用鼠标点击移动 隐藏/出现菜单栏 ctrl+鼠标控制字体大小 alt+O 弹出 ...
- shell命令行——快捷键
生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) ...
- Go知识点大纲
目录 1. 基本介绍 2. 安装及配置 3. 变量 4. 常量 5. 数据类型 5.1 numeric(数字) 5.2 string(字符串) 5.3 array(数组) 5.4 slice(切片) ...
- 简易kmeans-c++版本
typedef double dtype; 主要接口: void Kmeans(const vector<vector<dtype> > &d,int k,string ...
- RTSP, RTP, RTCP, RTMP傻傻分不清?
RTSP基于TCP传输请求和响应报文,RTP基于UDP传输流媒体数据,RTCP基于UDP传送传输质量信息(如丢包和延迟). 比如喀什一个局域网内10个人同时点播广州的同一个源,喀什和广州之间就要传10 ...
- volatile原理和应用场景
volatile是java语言中的一个关键字,常用于并发编程,有两个重要的特点:具有可见性,java虚拟机实现会为其满足Happens before原则;不具备原子性.用法是修饰变量,如:volati ...
- Spark的shuffle和MapReduce的shuffle对比
目录 MapperReduce的shuffle Spark的shuffle 总结 MapperReduce的shuffle shuffle阶段划分 Map阶段和Reduce阶段 任务 MapTask和 ...
- 练习1--爬取btc论坛的title和相应的url
爬不到此论坛的html源码,应该涉及到反爬技术,以后再来解决,代码如下 import requests from lxml import etree import json class BtcSpid ...
- 使用WtmPlus低代码平台提高生产力
低代码平台的概念很火爆,产品也是鱼龙混杂. 对于开发人员来说,在使用绝大部分低代码平台的时候都会遇到一个致命的问题:我在上面做的项目无法得到源码,完全黑盒.一旦我的需求平台满足不了,那就是无解. ...
- eclipse上安装 windowBuilder方法
最近因为需要用java Swing做一些组件设计,但想想以前在大学时候为了布局组件和位置设计花了很多时间.所以再网上查了一些带有可视化的设计插件用来提高工作效率. 其中一个是 windowBuilde ...