• 每日5题Day23 - LeetCode 111 - 115


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

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

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public int minDepth(TreeNode root) {
    18. //一次递归直接完成
    19. if(root == null){
    20. return 0;
    21. }
    22. //左边没子树了,走右边
    23. if(root.left == null){
    24. return minDepth(root.right) + 1;
    25. }
    26. //右边没子树了,走左边
    27. if(root.right == null){
    28. return minDepth(root.left) + 1;
    29. }
    30. //都有子树,选小的那边来返回
    31. return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    32. }
    33. }

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

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public boolean hasPathSum(TreeNode root, int targetSum) {
    18. //注意边界条件
    19. if(root == null){
    20. return false;
    21. }
    22. //到到达叶子节点的时候才判别
    23. if(root.left == null && root.right == null){
    24. return root.val - targetSum == 0;
    25. }
    26. //递归下去
    27. return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    28. }
    29. }

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

    1. class Solution {
    2. List> res = new LinkedList<>();
    3. List path = new LinkedList<>();
    4. public List> pathSum(TreeNode root, int targetSum) {
    5. if (root == null) {
    6. return res;
    7. }
    8. traversal(root, targetSum);
    9. return res;
    10. }
    11. private void traversal(TreeNode root, int targetSum) {
    12. path.add(root.val);
    13. if (root.left == null && root.right == null && root.val == targetSum) {
    14. res.add(new LinkedList<>(path));
    15. // 移除路径的最后一个节点
    16. path.remove(path.size() - 1);
    17. return;
    18. }
    19. if (root.left != null) {
    20. traversal(root.left, targetSum - root.val);
    21. }
    22. if (root.right != null) {
    23. traversal(root.right, targetSum - root.val);
    24. }
    25. // 移除路径的最后一个节点
    26. path.remove(path.size() - 1);
    27. }
    28. }

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

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public void flatten(TreeNode root) {
    18. //注意题目已经提示了,单链表是TreeNode的,顺序相当于中序遍历
    19. //只要是树咱们就考虑递归
    20. if(root == null){
    21. return;
    22. }
    23. //先左,优先级最高
    24. if(root.left != null){
    25. //一直往左找,遇到右就存起来
    26. TreeNode tmp = root.right;
    27. root.right = root.left;
    28. root.left = null;
    29. TreeNode current = root.right;
    30. while(current.right != null){
    31. current = current.right;
    32. }
    33. current.right = tmp;
    34. }
    35. flatten(root.right);
    36. }
    37. }

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

    1. class Solution {
    2. public int numDistinct(String s, String t) {
    3. int[][] dp = new int[s.length() + 1][t.length() + 1];
    4. for (int i = 0; i < s.length() + 1; i++) {
    5. dp[i][0] = 1;
    6. }
    7. for (int i = 1; i < s.length() + 1; i++) {
    8. for (int j = 1; j < t.length() + 1; j++) {
    9. if (s.charAt(i - 1) == t.charAt(j - 1)) {
    10. dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
    11. }else{
    12. dp[i][j] = dp[i - 1][j];
    13. }
    14. }
    15. }
    16. return dp[s.length()][t.length()];
    17. }
    18. }

  • 相关阅读:
    torch lighting 为不同模型设置不同的学习率
    阿里技术大牛耗时几个月整理出这份Spring Cloud Alibaba学习总结,值得学习呢
    app如何新增广告位以提升广告变现收益?
    Vatee万腾科技创新之舟:Vatee数字化力量引领未来的独特路径
    蓝桥杯模拟赛:最远滑行距离 ← dfs
    如何重新训练模型?
    H5+Vue3编写官网,并打包发布到同一个域名下
    macbook电脑删除app怎么才能彻底清理?
    浅谈嵌入式系统的持续集成
    30天Python入门(第四天:深入了解Python中的字符串)
  • 原文地址:https://blog.csdn.net/alimamaalala/article/details/139625725