摘要: leetcode:39. 组合总和 - 力扣(LeetCode) 思路:和组合一样,区别在于可以重复。如果递归里startIndex是starIndex的话,会发生后边都是由全部的数组成的,不是记录一个后往下一个走,这样就去重不了, class Solution { LinkedList<Integ 阅读全文
posted @ 2024-03-26 15:53 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: leetcode:216. 组合总和 III - 力扣(LeetCode) class Solution { List<List<Integer>> res = new ArrayList<>(); LinkedList<Integer> link = new LinkedList<>(); pub 阅读全文
posted @ 2024-03-24 14:44 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 回溯理论基础: 回溯三部曲:制定回溯函数的参数和返回值 确定回溯终止条件 确定回溯遍历过程 回溯模板 void backtracking(参数) { if (终止条件) { 存放结果; return; } for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) { 处理节点; back 阅读全文
posted @ 2024-03-20 13:24 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: leetcode:669. 修剪二叉搜索树 - 力扣(LeetCode) class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { //和删除差不多,怕删除的节点的左右孩子节点有符合范围的,所以要每次判断 阅读全文
posted @ 2024-03-17 16:12 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: leetcode:701. 二叉搜索树中的插入操作 - 力扣(LeetCode) class Solution { public TreeNode insertIntoBST(TreeNode root, int val) {//判断叶子结点,null说明到了,可以赋值。 if(root == nu 阅读全文
posted @ 2024-03-17 10:33 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: leetcode:530. 二叉搜索树的最小绝对差 - 力扣(LeetCode) 思路:判断最小绝对差,肯定用中序遍历,双指针一前一后依次判断。 class Solution { int result = Integer.MAX_VALUE; TreeNode pre = null; public 阅读全文
posted @ 2024-03-15 21:47 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: leetcode:654. 最大二叉树 - 力扣(LeetCode) 思路:要用数组找到最大二叉数,首先要找到最大值的下标,通过那个下标求分割数组,下标就是二叉数的根节点,将最大值添加到根节点后即可利用递归来用maxIndex分割左右二叉树。 class Solution { public Tree 阅读全文
posted @ 2024-03-13 21:24 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: leetcode:513. 找树左下角的值 - 力扣(LeetCode) 思路:是找最深左下角的值,不是找左节点最深的值!!遍历深度,判断最大深度,存储后再与下一个相同深度的比较,先左后右,也就是从左到右的顺序来判断的,所以能找到树下左下角的值 class Solution { int maxdep 阅读全文
posted @ 2024-03-12 15:15 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: leetcode:110. 平衡二叉树 - 力扣(LeetCode) class Solution { public boolean isBalanced(TreeNode root) { return getblan(root) != -1; } private int getblan(TreeN 阅读全文
posted @ 2024-03-10 14:26 22软工冷薄 阅读(1) 评论(0) 推荐(0) 编辑
摘要: leetcode:104. 二叉树的最大深度 - 力扣(LeetCode) 思路:递归判断每次左右节点的是否存在,存在自然加一,return的1就是这样,判断子节点的左右两端是否有节点,统计有的节点数量,也就是左右的高度 class Solution { public int maxDepth(Tr 阅读全文
posted @ 2024-03-09 14:08 22软工冷薄 阅读(2) 评论(0) 推荐(0) 编辑