力扣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;
    }
}
相关推荐
Forget the Dream11 分钟前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
LK_071 小时前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
Pniubi1 小时前
力扣21合并两个有序链表
算法·leetcode·职场和发展
黑十2 小时前
FDE 怎么快速搭一个可演示页面?最小前端方案
java·大数据·前端·数据库·人工智能
AIgorithmGEEK2 小时前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
神明不懂浪漫2 小时前
【第八章】多线程(一)
java·开发语言·经验分享·笔记
用户54817845909502 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
文丑颜不良啊2 小时前
LeetCode3341~3360题目解析
java·leetcode
hai_android3 小时前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
用户9338104348453 小时前
MyBatis-Plus LambdaQueryWrapper 隐式转换踩坑:不知不觉索引失效
java