前言
本文隶属于专栏《LeetCode 刷题汇总》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!
本专栏目录结构请见LeetCode 刷题汇总
Github 配套工程
正文
幕布
111. 二叉树的最小深度
题解
官方题解
DFS
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode111.solution1;
import com.shockang.study.algorithm.java.leetcode.common.TreeNode;
/**
* DFS
*
* @author Shockang
*/
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
int left = minDepth(root.left);
int right = minDepth(root.right);
if (left == 0 || right == 0) return left + right + 1;
return Math.min(left, right) + 1;
}
}
BFS
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode111.solution2;
import com.shockang.study.algorithm.java.leetcode.common.TreeNode;
import java.util.Deque;
import java.util.LinkedList;
/**
* BFS
*
* @author Shockang
*/
public class Solution {
public static int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
int level = 1;
while (!deque.isEmpty()) {
int size = deque.size();
for (int i = 0; i < size; i++) {
TreeNode cur = deque.poll();
if (cur.right == null && cur.left == null) {
return level;
}
if (cur.left != null) {
deque.offer(cur.left);
}
if (cur.right != null) {
deque.offer(cur.right);
}
}
level++;
}
return level;
}
}
112. 路径总和
题解
DFS
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode112.solution1;
import com.shockang.study.algorithm.java.leetcode.common.TreeNode;
/**
* DFS
*
* @author Shockang
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
if (sum - root.val == 0 && root.left == null && root.right == null) return true;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
BFS
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode112.solution2;
import com.shockang.study.algorithm.java.leetcode.common.TreeNode;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author Shockang
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
Queue<TreeNode> queNode = new LinkedList<>();
Queue<Integer> queVal = new LinkedList<>();
queNode.offer(root);
queVal.offer(root.val);
while (!queNode.isEmpty()) {
TreeNode now = queNode.poll();
int temp = queVal.poll();
if (now.left == null && now.right == null) {
if (temp == sum) {
return true;
}
continue;
}
if (now.left != null) {
queNode.offer(now.left);
queVal.offer(now.left.val + temp);
}
if (now.right != null) {
queNode.offer(now.right);
queVal.offer(now.right.val + temp);
}
}
return false;
}
}
113. 路径总和 II
题解
DFS,回溯
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode113.solution1;
import com.shockang.study.algorithm.java.leetcode.common.TreeNode;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
/**
* DFS
*
* @author Shockang
*/
public class Solution {
List<List<Integer>> ret = new LinkedList<>();
Deque<Integer> path = new LinkedList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
dfs(root, targetSum);
return ret;
}
public void dfs(TreeNode root, int targetSum) {
if (root == null) {
return;
}
path.offerLast(root.val);
targetSum -= root.val;
if (root.left == null && root.right == null && targetSum == 0) {
ret.add(new LinkedList<>(path));
}
dfs(root.left, targetSum);
dfs(root.right, targetSum);
path.pollLast();
}
}
114. 二叉树展开为链表
题解
官方题解
寻找前驱节点
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode114.solution1;
import com.shockang.study.algorithm.java.leetcode.common.TreeNode;
/**
* 寻找前驱节点
*
* @author Shockang
*/
public class Solution {
public void flatten(TreeNode root) {
TreeNode cur = root;
while (cur != null) {
if (cur.left != null) {
TreeNode next = cur.left;
TreeNode pre = next;
while (pre.right != null) {
pre = pre.right;
}
pre.right = cur.right;
cur.left = null;
cur.right = next;
}
cur = cur.right;
}
}
}
递归+反向思维+后序遍历
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode114.solution2;
import com.shockang.study.algorithm.java.leetcode.common.TreeNode;
/**
* 递归+反向思维+后序遍历
*
* @author Shockang
*/
public class Solution {
private TreeNode prev = null;
public void flatten(TreeNode root) {
if (root == null)
return;
flatten(root.right);
flatten(root.left);
root.right = prev;
root.left = null;
prev = root;
}
}
115. 不同的子序列
题解
动态规划
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode115.solution1;
/**
* 动态规划
*
* @author Shockang
*/
public class Solution {
public int numDistinct(String s, String t) {
int[][] dp = new int[t.length() + 1][s.length() + 1];
for (int j = 0; j < s.length() + 1; j++) dp[0][j] = 1;
for (int i = 1; i < t.length() + 1; i++) {
for (int j = 1; j < s.length() + 1; j++) {
if (t.charAt(i - 1) == s.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
else dp[i][j] = dp[i][j - 1];
}
}
return dp[t.length()][s.length()];
}
}