LeetCode 111~115

本文精选LeetCode上四道经典算法题目:二叉树的最小深度、路径总和、路径总和II及二叉树展开为链表的解题思路与代码实现,包括DFS和BFS等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

本文隶属于专栏《LeetCode 刷题汇总》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!

本专栏目录结构请见LeetCode 刷题汇总

Github 配套工程

algorithm

正文

幕布

在这里插入图片描述

幕布链接

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值