【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] ...
随机推荐
- 自动爬取代理IP例子
import time import json import datetime import threading import requests from lxml import etree from ...
- JVM监控工具之JVisualVM
一.简介 JVisualVM是Netbeans的profile子项目,已在JDK6.0 update 7 中自带(bin/jvisualvm.exe),能够监控线程,内存情况,查看方法的CPU时间和内 ...
- 6.824 Lab 3: Fault-tolerant Key/Value Service 3A
6.824 Lab 3: Fault-tolerant Key/Value Service Due Part A: Mar 13 23:59 Due Part B: Apr 10 23:59 Intr ...
- Chapter02 第二节 语句和变量
2.2 C++语句 2.11 声明语句和变量 示例程序: // carrots.cpp #include <bits/stdc++.h> using namespace std; int ...
- oracle表名中带@什么意思
例如:select * from dim.dim_area_no@to_dw @后是实例名或数据源,一个简单例子,服务器上创建了2个数据库实例,名称分别为HR.BOSS, 如果你用PL/SQL DEV ...
- keystone验证安装
以管理员的身份来请求鉴权的标识 keystone --os-tenant-name admin --os-username admin --os-password 123456 --os-auth- ...
- java8--- Optional的使用
// https://www.jianshu.com/p/82ed16613072 1.Optional.of(T value),传入非 null(否则会抛出 NullPointerException ...
- 2019JS必看面试题
2019JS必看面试题:https://www.jianshu.com/p/f1f39d5b2a2e 1. javascript的typeof返回哪些数据类型. 答案:string,boolean,n ...
- adb,aapt等命令使用
adb install/uninstall:安装/卸载手机中的应用. devices:查看当前连接到电脑中的设备. adb shell 首先运行adb ...
- 03: 使用docker搭建Harbor私有镜像仓库
1.1 harbor介绍 1.Harbor简介 1. Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器. 2. 镜像的存储harbor使用的是官方的docker regi ...