题目地址: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:

  1. The number of nodes in the given tree is at most 1000.
  2. Each node has a distinct value between 1 and 1000.
  3. to_delete.length <= 1000
  4. to_delete contains distinct values between 1 and 1000.

题目大意

删除一棵二叉树中的所有值出现在to_delete中的节点。

解题方法

递归

参考了lee215大神的答案。看到二叉树的题就想到递归呀!

一个节点被删除时有以下几个情况:

  1. 如果该节点是根节点,形成左右两个子树,此时递归左右子树。
  2. 如果该节点不是根节点,那么需要修改其父节点指向自己的指针为空,并且递归左右子树。

一个节点一旦被删除,那么其左右孩子就是新的树的根节点。
如果一个节点是根节点,并且不被删除的情况下,才会放入结果中。

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++)的更多相关文章

  1. LeetCode 1110. Delete Nodes And Return Forest

    原题链接在这里:https://leetcode.com/problems/delete-nodes-and-return-forest/ 题目: Given the root of a binary ...

  2. 【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 ...

  3. 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 ...

  4. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  5. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  6. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  7. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

随机推荐

  1. jupyter 远程访问

    Jupyter 远程访问 jupyter 远程访问的工作方法是,在本地通过浏览器打开jupyter,但是代码和服务运行在远程集群中. 集群设置 首先需要确保集群中安装有python和jupyter. ...

  2. git创建项目,代码仓库

    1.首先在服务端远程创建仓库 mkdir  project.git cd  project.git git  --bare init 2.在本地创建项目推送到远程服务端仓库 mkdir  myproj ...

  3. 关于JSONObject的性能问题

    现有一段代码: private JSONObject override(User user, UserVO vo) { String json = JSON.toJSONString(vo); JSO ...

  4. Spark(二十一)【SparkSQL读取Kudu,写入Kafka】

    目录 SparkSQL读取Kudu,写出到Kafka 1. pom.xml 依赖 2.将KafkaProducer利用lazy val的方式进行包装, 创建KafkaSink 3.利用广播变量,将Ka ...

  5. Vue相关,Vue生命周期及对应的行为

    先来一张经典图 生命钩子函数 使用vue的朋友们知道,生命周期函数长这样- mounted: function() { } // 或者 mounted() { } 注意点,Vue的所有生命周期函数都是 ...

  6. oracle 执行计划的获取方法

    1.用explain plan for来获取执行计划 explain plan for <sql>; select * from table(dbms_xplan.display()); ...

  7. 在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题

    在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题 此时解决办法是 #pragma mark - UIImagePickerController D ...

  8. 应用springMVC时如果配置URL映射时如下配置

    应用springMVC时如果配置URL映射时如下配置 [html] view plaincopy<servlet> <servlet-name>appServlet</s ...

  9. 【Linux】【Services】【DNS】使用Bind搭建DNS服务

    1. 简介 1.1. 实现的功能:DNS解析以及智能转发 1.2. 官方文档: 1.3. 基础概念:http://www.cnblogs.com/demonzk/p/6494968.html 2. 环 ...

  10. 【Linux】【Shell】【text】awk

    基本用法:gawk [options] 'program' FILE ...             program: PATTERN{ACTION STATEMENTS}               ...