摘要: 2024年7月25日 题39. 组合总和 由于每个元素可以用多次,要想到在每次递归里还要循环即可。 代码首先给各个候选排序,从小到大依次提高门槛,每次回溯就提高index。 class Solution { List<List<Integer>> res; List<Integer> path; i 阅读全文
posted @ 2024-07-27 14:10 hailicy 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 2024年7月24日 回溯入门 用for来遍历每个元素,然后进入递归。 题77. 组合 给出n和k,可选元素1到n,k是组合个数。 易错:添加进外层list时要new一个,否则后续修改会影响已经添加的内层list class Solution { List<List<Integer>> list1; 阅读全文
posted @ 2024-07-24 10:19 hailicy 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 2024年7月23日 题669. 修剪二叉搜索树 暴力做法是找出所有不符合的节点再一一删除。 import java.util.*; class Solution { ArrayList<Integer> list; public TreeNode trimBST(TreeNode root, in 阅读全文
posted @ 2024-07-24 08:56 hailicy 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 2024年7月22日 题235. 二叉搜索树的最近公共祖先 普通解法还是和普通的祖先一样求即可,再依据搜索树特性进行剪枝即可加速。 import java.util.*; class Solution { Vector<TreeNode> vec1; Vector<TreeNode> vec2; i 阅读全文
posted @ 2024-07-23 16:05 hailicy 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 2024年7月20日 题530. 二叉搜索树的最小绝对差 使用递归获取中序遍历,然后遍历一遍vector即可得到结果。 import java.util.*; class Solution { Vector<Integer> vec; public int getMinimumDifference( 阅读全文
posted @ 2024-07-23 15:23 hailicy 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 2024年7月19日 题654. 最大二叉树 熟练运用递归即可 class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { int maxNum = Integer.MIN_VALUE; int flag=-1; 阅读全文
posted @ 2024-07-22 16:16 hailicy 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 2024年7月18日 用层序遍历巧解 题513. 找树左下角的值 层序遍历的板子一定要熟背。 class Solution { public int findBottomLeftValue(TreeNode root) { List<List<Integer>> res = new ArrayLis 阅读全文
posted @ 2024-07-21 18:08 hailicy 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 2024年7月17日 平衡二叉树 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * Tr 阅读全文
posted @ 2024-07-21 17:36 hailicy 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 2024年7月16日 题226. 翻转二叉树 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() { 阅读全文
posted @ 2024-07-17 14:53 hailicy 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 2024年7月15日 二叉树 前序遍历,前就是指根在前,递归要注意判断节点为空。 如果不用递归,就用一个栈来保存节点。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode l 阅读全文
posted @ 2024-07-15 21:56 hailicy 阅读(0) 评论(0) 推荐(0) 编辑