题目如下:

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 and 1000.
  • to_delete.length <= 1000
  • to_delete contains distinct values between 1 and 1000.

解题思路:从根节点开始,判断是否在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的更多相关文章

  1. 【LeetCode】1110. Delete Nodes And Return Forest 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  2. LeetCode 1110. Delete Nodes And Return Forest

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

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

  4. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

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

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

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

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

  9. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

随机推荐

  1. 阶段3 1.Mybatis_07.Mybatis的连接池及事务_4 mybatis中使用unpooled配置连接池的原理分析

    把之前的CRUD的代码src下的代码都复制过来 依赖项也都复制过来, 配置文件 整理一番 执行findAll方法的测试 查看日志的输出部分 修改程序池 再来执行findAll方法 Plooled从连接 ...

  2. keepalive + nginx 搭建高可用集群动态网站

    环境准备: 两台节点部署keepalived,并且设为互为主从,实现高可用. 两台从节点部署nginx以及相关组件,作为真实服务器实现动态网站上线. 一.MASTER(BACKUP)节点下载keepa ...

  3. Struts2基本流程

    转载:https://www.cnblogs.com/wkrbky/p/5894174.html 概述: Struts2框架由三部分构成:核心控制器.业务控制器和用户实现的业务逻辑组件.在这三部分中, ...

  4. powered by Fernflower decompiler

    About Fernflower Fernflower is the first actually working analytical decompiler for Java and probabl ...

  5. struts2 2.5.16 通配符方式调用action中的方法报404

    1.问题描述 在struts.xml中配置用通配符方式调用action中的add()方法,访问 http://localhost:8080/Struts2Demo/helloworld_add.act ...

  6. mysql5.6修改密码并授权所有远程用户可登陆

    1.my.ini文件,删除最后一行的"skip-grant-tables 2.执行"use mysql;",使用mysql数据库; 3.执行:update mysql.u ...

  7. 学了一天的golang从入门到放弃

    Google的go就是个二货,不实用,它最多只能和c比简单.low!

  8. vue2.0父子组件通信以及同级组件通信

    1.父向子通信 父组件为singer.vue.子组件为list-view.vue.需要把歌手的数据传给子组件.则绑定 :data = 'singers' ,singers为父组件的值.data为子组件 ...

  9. base64 换表 解密脚本

    做逆向经常遇到换表的base64 有了py脚本 一切都好说: import base64 import string str1 = "x2dtJEOmyjacxDemx2eczT5cVS9f ...

  10. 极*Java速成教程 - (7)

    Java高级特性 数组 在Java中,数组是一串连续的,不可改变长度的,对象被固定的,类型固定的连续空间.数组中的随机访问非常迅速,但为了速度放弃了灵活性.而效率也是数组最大的优点. 在使用泛型的容器 ...