LeetCode 1110. Delete Nodes And Return Forest
原题链接在这里: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
and1000
. to_delete.length <= 1000
to_delete
contains distinct values between1
and1000
.
题解:
For DFS state, we need current node, to_delete set and List<TreeNode> res.
The return should be TreeNode.
It uses divide and conquer, which is bottom-up DFS.
Comes to the bottom leaf node, if it is null, return null.
otherwise, check if the current node is to be deleted.
If yes, then its children need to be added to res.
Time Complexity: O(n). n is tree size.
Space: O(logn + m) m = to_delete.length.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
List<TreeNode> res = new ArrayList<>();
HashSet<Integer> hs = new HashSet<>();
for(int n : to_delete){
hs.add(n);
} root = dfs(root, hs, res);
if(root != null){
res.add(root);
} return res;
} private TreeNode dfs(TreeNode root, HashSet<Integer> hs, List<TreeNode> res){
if(root == null){
return root;
} root.left = dfs(root.left, hs, res);
root.right = dfs(root.right, hs, res);
if(hs.contains(root.val)){
if(root.left != null){
res.add(root.left);
} if(root.right != null){
res.add(root.right);
} return null;
} return root;
}
}
LeetCode 1110. Delete Nodes And Return Forest的更多相关文章
- 【LeetCode】1110. Delete Nodes And Return Forest 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【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 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [LeetCode] 24. Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Java for LeetCode 024 Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- leetcode:Swap Nodes in Pairs
Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
随机推荐
- 配置 Nginx 反向代理 WebSocket
用Nginx给网站做反向代理和负载均衡是广泛使用的一种Web服务器部署技术.不仅能够保证后端服务器的隐蔽性,还可以提高网站部署灵活性. 今天我们来讲一下,如何用Nginx给WebSocket服务器实现 ...
- syntax error near unexpected token 脚本报错误解决
hadoop老师给了一个shell文件,在windows里面瞅了一眼然后在ubuntu环境下运行就报错了.看了一些博客,用vim -b filename查看的时候发现每一行的末尾都多了一个^M.... ...
- ASP.NET MVC实现单用户登录
现在许多网站都要求登录后才能进行进一步的操作,当不允许多用户同时登录一个帐号时,就需要一种机制,当再登录一个相同的帐号时,前面登录的人被挤下线,或者禁止后面的人登录.这里实现的是前一种功能. 网上有许 ...
- C#完成 使用异步线程定时更新窗体标签内容,并对标签内容进行求和显示
这是我在面试过程中遇到的一个小测试,很可惜,当天未能圆满完成,虽然第二天经过实际测试已经OK 了,但学无止境,继续努力吧,特将此代码贴在这里,供以后学习使用: namespace mytest { p ...
- java 枚举示例
public enum YNEnum { N(0,"否"), Y(1,"是"); private int code; private String name; ...
- Spring Security 解析(六) —— 基于JWT的单点登陆(SSO)开发及原理解析
Spring Security 解析(六) -- 基于JWT的单点登陆(SSO)开发及原理解析 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...
- Django---FORM组件.FORM组件的字段,FORM组件校验流程,FORM组件的全局和局部钩子,FORM和Model的组合
Django---FORM组件.FORM组件的字段,FORM组件校验流程,FORM组件的全局和局部钩子,FORM和Model的组合 一丶FORM的介绍 1.生成页面可用的HTML标签 2.对用户提交的 ...
- pandas-17 关于nan的处理
pandas-17 关于nan的处理 在pandas中有个另类的存在就是nan,解释是:not a number,不是一个数字,但是它的类型确是一个float类型.numpy中也存在关于nan的方法, ...
- elementUI一次请求上传多个文件
elementui <el-upload class="upload-demo" ac ...
- 基于JPA的分页/排序实现
Page<ClassOrder> findByMember_MemberID(long id, Pageable pageable); Controller代码: public Model ...