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-& ...
随机推荐
- 面试题(Python)
面试题 字符串反向输出 s = "给阿姨倒杯卡布奇诺"反向输出S:print(s[::-1]) 面试必问:赋值,浅拷贝,深拷贝 赋值:多个变量指到相同内存浅拷贝中所有的元素,不管第 ...
- java数字前面补充0公共方法
- c# .netcore oracle连接工具类
1.先右键->添加NeGet包->引入Oracle.ManagedDataAccess.dll 2.将该类加入项目中 工具类: using System; using System.Col ...
- sql server union与unionALL区别
两种用法 一样, 查询字段类型需要一致 union 会自动去重 union all 不会去重 select name ,age from student union select name ,age ...
- C# zip压缩 Ionic.Zip.dll
#region Ionic.Zip压缩文件 //压缩方法一 public void ExeCompOne() { string FileName = DateTime.Now.ToString(&qu ...
- easydict的使用方法
easydict的作用:可以使得以属性的方式去访问字典的值 from easydict import EasyDict as edict a=['8',2,3]a=edict()a.f=99print ...
- JAVA 架构和技术框架百科
YApi 是高效.易用.功能强大的 api 管理平台,旨在为开发.产品.测试人员提供更优雅的接口管理服务.可以帮助开发者轻松创建.发布.维护 API,YApi 还为用户提供了优秀的交互体验,开发人员只 ...
- Vue学习之Webpack小结(十二)
一.nrm: nrm是专门用来管理和快速切换私人配置的registry; nrm提供了一些最常用的npm包镜像地址,能够让我们快速的切换安装包时候的服务器地址: 二.镜像: 原来 包 刚一开 ...
- odex vdex art区别
一.vdexpackage 直接转化的 可执行二进制码 文件:1.第一次开机就会生成在/system/app/<packagename>/oat/下:2.在系统运行过程中,虚拟机将其 从 ...
- FFMPEG 命令行工具- ffmpeg
ffmpeg 简介 ffmpeg 用于转码的应用程序,命令格式: ffmpeg [options] [[infile options] -i infile]... {[outfile options] ...