# 每日5题Day23 - LeetCode 111 - 115

24 篇文章 1 订阅

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

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

class Solution {
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) {
if (root.left == null && root.right == null && root.val == targetSum) {
// 移除路径的最后一个节点
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);
}
}


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

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()];
}
}

• 5
点赞
• 1
收藏
觉得还不错? 一键收藏
• 打赏
• 1
评论
06-08 401
06-03 210
06-10 404
06-09 506
06-13 503
06-03 249
06-02 369
05-31 401
06-12 472
07-15 109
07-14 241
07-14 898
07-14 351

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。