每日5题Day23 - LeetCode 111 - 115

每一步向前都是向自己的梦想更近一步,坚持不懈,勇往直前!

第一题:111. 二叉树的最小深度 - 力扣(LeetCode)

/**
 * 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 int minDepth(TreeNode root) {
        //一次递归直接完成
        if(root == null){
            return 0;
        }
        //左边没子树了,走右边
        if(root.left == null){
            return minDepth(root.right) + 1;
        }
        //右边没子树了,走左边
        if(root.right == null){
            return minDepth(root.left) + 1;
        }
        //都有子树,选小的那边来返回
        return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    }
}

第二题:112. 路径总和 - 力扣(LeetCode)

/**
 * 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 boolean hasPathSum(TreeNode root, int targetSum) {
        //注意边界条件
        if(root == null){
            return false;
        }
        //到到达叶子节点的时候才判别
        if(root.left == null && root.right == null){
            return root.val - targetSum == 0;
        }
        //递归下去
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }
}

第三题:113. 路径总和 II - 力扣(LeetCode)

class Solution {
    List<List<Integer>> res = new LinkedList<>();
    List<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return res;
        }
        traversal(root, targetSum);
        return res;
    }

    private void traversal(TreeNode root, int targetSum) {
        path.add(root.val);
        if (root.left == null && root.right == null && root.val == targetSum) {
            res.add(new LinkedList<>(path));
            // 移除路径的最后一个节点
            path.remove(path.size() - 1); 
            return;
        } 
        if (root.left != null) {
            traversal(root.left, targetSum - root.val);
        }
        if (root.right != null) {
            traversal(root.right, targetSum - root.val);
        }
        // 移除路径的最后一个节点
        path.remove(path.size() - 1); 
    }
}

第四题:114. 二叉树展开为链表 - 力扣(LeetCode)

/**
 * 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 void flatten(TreeNode root) {
        //注意题目已经提示了,单链表是TreeNode的,顺序相当于中序遍历
        //只要是树咱们就考虑递归
        if(root == null){
            return;
        }
        //先左,优先级最高
        if(root.left != null){
            //一直往左找,遇到右就存起来
            TreeNode tmp = root.right;
            root.right = root.left;
            root.left = null;
            
            TreeNode current = root.right;
            while(current.right != null){
                current = current.right;
            }
            current.right = tmp;
        }
        flatten(root.right);
    }
}

 第五题:115. 不同的子序列 - 力扣(LeetCode)

class Solution {
    public int numDistinct(String s, String t) {
        int[][] dp = new int[s.length() + 1][t.length() + 1];
        for (int i = 0; i < s.length() + 1; i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i < s.length() + 1; i++) {
            for (int j = 1; j < t.length() + 1; j++) {
                if (s.charAt(i - 1) == t.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                }else{
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return dp[s.length()][t.length()];
    }
}

相关推荐

  1. LeetCode 每日 Day 11||贪心

    2024-06-13 02:12:01       44 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 02:12:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 02:12:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 02:12:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 02:12:01       20 阅读

热门阅读

  1. B. Choosing Cubes

    2024-06-13 02:12:01       7 阅读
  2. 4.MongoDB sharding Cluster 分片集群

    2024-06-13 02:12:01       9 阅读
  3. mongo数据迁移方法

    2024-06-13 02:12:01       7 阅读
  4. 防护DDoS攻击出现的常见误区

    2024-06-13 02:12:01       5 阅读
  5. moocast(usaco2016年12月金组第1题)

    2024-06-13 02:12:01       8 阅读
  6. c#与汇川plc通信

    2024-06-13 02:12:01       9 阅读
  7. #07【面试问题整理】嵌入式软件工程师

    2024-06-13 02:12:01       9 阅读
  8. leetcode hot100 之 最长公共子序列

    2024-06-13 02:12:01       7 阅读
  9. SSRF-gopher 协议扩展利用:突破网络限制的利器

    2024-06-13 02:12:01       7 阅读
  10. Ant-Design-Vue 动态表头

    2024-06-13 02:12:01       9 阅读