力扣labuladong——一刷day52

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

文章目录

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


前言


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

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

/**
 * 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

/**
 * 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. 删点成林

/**
 * 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;
    }
}

你可能感兴趣的:(力扣题解,leetcode,算法,职场和发展,java,数据结构)