【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 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
.
解题思路:从根节点开始,判断是否在to_delete,如果不在,把这个节点加入Output中,往左右子树方向继续遍历;如果在,把其左右子节点加入queue中;而后从queue中依次读取元素,并对其做与根节点一样的操作,直到queue为空位置。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def recursive(self,node,queue,to_delete):
if node.left != None and node.left.val in to_delete:
queue.append(node.left)
node.left = None
if node.right != None and node.right.val in to_delete:
queue.append(node.right)
node.right = None
if node.left != None:
self.recursive(node.left,queue,to_delete)
if node.right != None:
self.recursive(node.right,queue,to_delete)
def delNodes(self, root, to_delete):
"""
:type root: TreeNode
:type to_delete: List[int]
:rtype: List[TreeNode]
"""
if root == None:
return []
queue = [root]
res = []
while len(queue) > 0:
node = queue.pop(0)
if node.val not in to_delete:
res.append(node)
self.recursive(node,queue,to_delete)
else:
if node.left != None:
queue.append(node.left)
if node.right != None:
queue.append(node.right) return res
【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
原题链接在这里:https://leetcode.com/problems/delete-nodes-and-return-forest/ 题目: Given the root of a binary ...
- 【leetcode】955. Delete Columns to Make Sorted II
题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【LeetCode】024. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】1273. Delete Tree Nodes
题目如下: A tree rooted at node 0 is given as follows: The number of nodes is nodes; The value of the i- ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
随机推荐
- CentOS6.5 编译安装PHP5.6(apache模块)
一.环境准备 1. 下载php源码包 # wget http://cn2.php.net/distributions/php-5.6.30.tar.gz # tar -xf php-5.6.30.ta ...
- FacertGrid()的使用
查看数据的前五行 tips = sns.load_dataset("tips") tips.head() 引入数据,布置横向画布 g = sns.FacetGrid(tips, c ...
- 【计算机视觉】HDR之tone mapping简介
tone Mapping原是摄影学中的一个术语,因为打印相片所能表现的亮度范围不足以表现现实世界中的亮度域,而如果简单的将真实世界的整个亮度域线性压缩到照片所能表现的亮度域内,则会在明暗两端同时丢失很 ...
- 【Qt开发】【Linux开发】QT设置环境变量QWS_DISPLAY
QT设置环境变量QWS_DISPLAY 当应用程序./myQtApp -qws启动时,会去检测QWS_DISPLAY这个环境变量, 判断界面最终显示在哪个framebuffer中, 如果是虚拟的fra ...
- netcore 使用redis session 分布式共享
首先准备redis服务器(docker 和redis3.0内置的哨兵进行高可用设置) 网站配置Redis作为存储session的介质(配置文件这些略).然后可以了解一下MachineKey这个东西.( ...
- python+selenium模拟键盘输入
from selenium.webdriver.common.keys import Keys #键盘导入类 --------------------------------------------- ...
- JAVA poi设置单元格背景颜色
import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Ce ...
- P1311选择客栈
这是2011年提高组D1T2,是一个绿色的模拟题,不出所料,没写出代码来. 首先输入n个客栈的颜色和最低消费,然后根据“同颜色但不是一个客栈”以及“两个客栈之间必须有一个的最低消费<=p&quo ...
- 【LGR-065】洛谷11月月赛 III Div.2
临近$CSP$...... 下午打了一发月赛,感觉很爽. 非常菜的我只做了前两题......然而听说前两题人均过...... 写法不优秀被卡到$#1067$...... T1:基础字符串练习题: 前缀 ...
- Excel如何通过关键字模糊匹配查找全称
打开excel,以其素材为例,通过关键字模糊匹配查找全称. 在公司名下输入公式:=LOOKUP(1,0/FIND(D2,A2:A5),A2:A5),按回车键确定即可. FIND(D2,A2:A ...