Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

  • 链表

  • 字符串

  • 二叉树

    • 102.二叉树的层序遍历
    • 103.二叉树的锯齿形层序遍历
    • 124.二叉树中的最大路径和
    • 236.二叉树的最近公共祖先
    • 105.从前序与中序遍历序列构造二叉树
    • 662.二叉树最大宽度
    • 1080. 根到叶路径上的不足节点
    • 101. 对称二叉树
    • 226. 翻转二叉树
    • 114. 二叉树展开为链表
    • 1110. 删点成林
      • 1.DFS
    • 337. 打家劫舍 III
    • 979. 在二叉树中分配硬币
    • 222. 完全二叉树的节点个数
    • 513. 找树左下角的值
    • 968. 监控二叉树
    • 剑指offer34
    • 剑指 Offer 32 - III. 从上到下打印二叉树 III
    • 剑指 Offer 33. 二叉搜索树的后序遍历序列
    • 线索二叉树

    • 二叉搜索树

  • 动态规划

  • 深搜回溯

  • 数学贪心

  • 堆栈队列

  • 前缀和

  • 算法设计

  • 位运算

  • WA

  • 算法
  • 二叉树
phan
2023-05-30
目录

1110. 删点成林

# 1110. 删点成林 (opens new window)

# 1.DFS

当前节点在删除节点列表时,除了要删除该节点与孩子节点的关联之外,还需要删除该节点与父节点之间的关联。因此搜索时当前节点删除不了父节点关联,需要交给父节点进行删除(每个节点都要判断子节点是否在删除列表当中)。另外搜索时,无论当前节点是否在删除列表当中,都需要递归搜索左右子树所有节点。

另外搜索过程中,虽然不能判断一个节点是否是非删除子树的根节点,但是加入的非删除子树根节点一定满足一条性质:它的父亲一定在删除队列当中。

  • 如果当前节点不在删除队列,①遍历左右子树②删除左右子树关联
  • 当前节点在删除队列,①删除左右子树关联②遍历左右子树③如果左右子树不在删除列表,则加入结果集中。

class Solution {
    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
            List<Integer> list=new ArrayList<>();
            List<TreeNode> res=new ArrayList<>();
            for(int i:to_delete)
            list.add(i);
            if(!list.contains(root.val)) res.add(root);
            dfs(res,list,root);
            return res;
    }
    public void dfs(List<TreeNode> res,List<Integer> list,TreeNode root){
        if(root==null) return ;
        if(!list.contains(root.val)){
            dfs(res,list,root.left);
            dfs(res,list,root.right);
            if(root.left!=null&&list.contains(root.left.val)) root.left=null;
            if(root.right!=null&&list.contains(root.right.val)) root.right=null;
        }else{
            if(root.left!=null){
                TreeNode leftchild=root.left;
                root.left=null;
                if(!list.contains(leftchild.val)) res.add(leftchild);
                dfs(res,list,leftchild);

            }
            if(root.right!=null){
                TreeNode rightchild=root.right;
                root.right=null;
                if(!list.contains(rightchild.val))res.add(rightchild);
                dfs(res,list,rightchild);

            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
编辑 (opens new window)
#Leetcode#二叉树
上次更新: 2023/12/15, 15:49:57
114. 二叉树展开为链表
337. 打家劫舍 III

← 114. 二叉树展开为链表 337. 打家劫舍 III→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式