力扣labuladong——一刷day52

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣894. 所有可能的真二叉树](#一、力扣894. 所有可能的真二叉树)
  • [二、力扣998. 最大二叉树 II](#二、力扣998. 最大二叉树 II)
  • [三、力扣1110. 删点成林](#三、力扣1110. 删点成林)

前言


二叉树的递归分为「遍历」和「分解问题」两种思维模式,这道题需要用到「分解问题」的思维。

一、力扣894. 所有可能的真二叉树

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<TreeNode>[] memo;
    public List<TreeNode> allPossibleFBT(int n) {
        if(n % 2 == 0){
            return new LinkedList<>();
        }
        memo = new LinkedList[n+1];
        return build(n);
    }
    public List<TreeNode> build(int n){
        List<TreeNode> res = new LinkedList<>();
        if(n == 1){
            res.add(new TreeNode(0));
            return res;
        }
        if(memo[n] != null){
            return memo[n];
        }
        for(int i = 1; i < n; i += 2){
            int j = n -i -1;
            List<TreeNode> l = build(i);
            List<TreeNode> r = build(j);
            for(TreeNode lt : l){
                for(TreeNode lr : r){
                    TreeNode cur = new TreeNode(0);
                    cur.left = lt;
                    cur.right = lr;
                    res.add(cur);
                }
            }
        }
        memo[n] = res;
        return res;
    }
}

二、力扣998. 最大二叉树 II

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode insertIntoMaxTree(TreeNode root, int val) {
        if(root == null){
            return new TreeNode(val);
        }
        if(root.val < val){
            TreeNode temp = root;
            root = new TreeNode(val);
            root.left = temp;
            return root;
        }else{
            root.right = insertIntoMaxTree(root.right,val);
            return root;
        }
    }
}

三、力扣1110. 删点成林

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    HashSet<Integer> delSet = new HashSet<>();
    List<TreeNode> res = new LinkedList<>();
    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
        if(root == null){
            return new LinkedList<>();
        }
        for(int i : to_delete){
            delSet.add(i);
        }
        fun(root,false);
        return res;
    }
    public TreeNode fun(TreeNode root, boolean hisParent){
        if(root == null){
            return null;
        }
        boolean isdelete = delSet.contains(root.val);
        if(!isdelete && !hisParent){
            res.add(root);
        }
        root.left = fun(root.left,!isdelete);
        root.right = fun(root.right,!isdelete);
        return isdelete ? null:root;
    }
}
相关推荐
CS创新实验室33 分钟前
从盘边到芯端——硬盘接口七十年变迁史
算法·磁盘调度
TeDi TIVE40 分钟前
springboot和springframework版本依赖关系
java·spring boot·后端
二哈赛车手40 分钟前
新人笔记---ES和kibana启动问题以及一些常用的linux的错误排查方法,以及ES,数据库泄密解决方案[超详细]
java·linux·数据库·spring boot·笔记·elasticsearch
嵌入式×边缘AI:打怪升级日志1 小时前
嵌入式Linux开发核心自测题(全系列精华浓缩)
java·linux·运维
xvhao20131 小时前
单源、多源最短路
数据结构·c++·算法·深度优先·动态规划·图论·图搜索算法
MATLAB代码顾问1 小时前
多种群协同进化算法(MPCE)求解大规模作业车间调度问题——附MATLAB代码
开发语言·算法·matlab
FQNmxDG4S1 小时前
JVM内存模型详解:堆、栈、方法区与垃圾回收
java·jvm·算法
We་ct2 小时前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·leetcode·typescript·动态规划
AI科技星2 小时前
精细结构常数α作为SI 7大基本量纲统一耦合常数的量子几何涌现理论
算法·机器学习·数学建模·数据挖掘·量子计算
jason.zeng@15022072 小时前
Androidr入门环境搭建
java·kotlin