代码随想录算法训练营第十八天| 513. 找树左下角的值 112. 路径总和 113. 路径总和 II 106. 从中序与后序遍历序列构造二叉树 105. 从前序与中序遍历序列构造二叉树
1.代码随想录算法训练营第二天| 977.有序数组的平方、 209.长度最小的子数组、 59.螺旋矩阵II2.代码随想录算法训练营第三天| 203. 移除链表元素 707.设计链表 206.反转链表3.代码随想录算法训练营第四天| 24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07. 链表相交 142. 环形链表 II4.代码随想录算法训练营第一天| 704. 二分查找、27. 移除元素。5.代码随想录算法训练营第六天| 242. 有效的字母异位词 349. 两个数组的交集 202. 快乐数 1. 两数之和6.代码随想录算法训练营第七天| 454. 四数相加 II 383. 赎金信 15. 三数之和 18. 四数之和7.代码随想录算法训练营第九天| 28. 实现 strStr() 和讲讲KMP8.代码随想录算法训练营第十天| 232. 用栈实现队列 225. 用队列实现栈9.代码随想录算法训练营第十一天| 20. 有效的括号 1047. 删除字符串中的所有相邻重复项 150. 逆波兰表达式求值10.代码随想录算法训练营第十三天| 239. 滑动窗口最大值 347. 前 K 个高频元素11.代码随想录算法训练营第十四天| 二叉树相关递归遍历以及迭代遍历12.代码随想录算法训练营第十五天| 226. 翻转二叉树 101. 对称二叉树13.代码随想录算法训练营第十六天| 104. 二叉树的最大深度 111. 二叉树的最小深度 222. 完全二叉树的节点个数14.代码随想录算法训练营第十七天| 110. 平衡二叉树 257. 二叉树的所有路径 404. 左叶子之和
15.代码随想录算法训练营第十八天| 513. 找树左下角的值 112. 路径总和 113. 路径总和 II 106. 从中序与后序遍历序列构造二叉树 105. 从前序与中序遍历序列构造二叉树
16.代码随想录算法训练营第二十一天,二十二,二十三| 530. 二叉搜索树的最小绝对差 501. 二叉搜索树中的众数 236. 二叉树的最近公共祖先 235. 二叉搜索树的最近公共祖先 701. 二叉搜索树中的插入操作 450. 删除二叉搜索树中的节点17.代码随想录算法训练营第二十四天 二十五 | 回溯的理论基础,77. 组合 216. 组合总和 III 17. 电话号码的字母组合18.代码随想录 | 图论 797. 所有可能的路径(dfs) ,200. 岛屿数量 (dfs)200. 岛屿数量 (bfs) ,并查集,1971. 寻找图中是否存在路径513. 找树左下角的值
https://leetcode.cn/problems/find-bottom-left-tree-value/description/
public int findBottomLeftValue(TreeNode root) {
int val = 0;
Deque<TreeNode> deque = new ArrayDeque<>();
deque.offer(root);
while (!deque.isEmpty()){
int len = deque.size();
for (int i = 0; i < len; i++) {
TreeNode node = deque.poll();
if (i == 0) val = node.val;
if (node.left != null) deque.offer(node.left);
if (node.right != null) deque.offer(node.right);
}
}
return val;
}
总结:层序遍历
112. 路径总和
https://leetcode.cn/problems/path-sum/description/
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
boolean sum = sum(root, targetSum);
return sum;
}
public boolean sum(TreeNode node,int sum){
if (node.left == null && node.right == null) {
return sum - node.val == 0; // 到达叶子节点时判断路径和是否等于目标和
}
boolean sumLeft = false;
boolean sumRight = false;
if (node.left != null) {
sumLeft = sum(node.left, sum - node.val);
}
if (node.right != null){
sumRight = sum(node.right, sum - node.val);
}
return sumLeft || sumRight;
}
总结:递归到叶子节点,判断是否合理
113. 路径总和 II
https://leetcode.cn/problems/path-sum-ii/description/
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> resList = new ArrayList<>();
List<Integer> path = new ArrayList<>();
if (root == null) return resList;
pathSSum(root,targetSum,resList,path);
return resList;
}
public void pathSSum(TreeNode node,int targetSum,List<List<Integer>> resList,List<Integer> path){
if (node.left == null && node.right == null){
if (targetSum - node.val == 0){
path.add(node.val);
resList.add(new ArrayList<>(path));
path.remove(path.size() - 1); //回溯
}
return;
}
if (node.left != null){
path.add(node.val);
pathSSum(node.left,targetSum - node.val,resList,path);
path.remove(path.size() - 1); //回溯
}
if (node.right != null){
path.add(node.val);
pathSSum(node.right,targetSum - node.val,resList,path);
path.remove(path.size() - 1); //回溯
}
}
总结:带了点回溯
106. 从中序与后序遍历序列构造二叉树
https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/
int post_idx;
int[] postorder;
int[] inorder;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
this.postorder = postorder;
this.inorder = inorder;
// 从后序遍历的最后一个元素开始
post_idx = postorder.length - 1;
// 建立(元素,下标)键值对的哈希表
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i],i);
}
return helper(0, inorder.length - 1);
}
public TreeNode helper(int in_left, int in_right) {
// 如果这里没有节点构造二叉树了,就结束
if (in_left > in_right) {
return null;
}
// 选择 post_idx 位置的元素作为当前子树根节点
int root_val = postorder[post_idx];
TreeNode root = new TreeNode(root_val);
// 根据 root 所在位置分成左右两棵子树
int index = map.get(root_val);
// 下标减一
post_idx--;
// 构造右子树
root.right = helper(index + 1, in_right);
// 构造左子树
root.left = helper(in_left, index - 1);
return root;
}
总结:力扣官方的代码,递归中每次传中序数组的左右边界,每次递归把当前的root拿出来去给他构造left,right。返回当前root,注意这里要先构造右子树,再左子树,因为后序数组的遍历是先右再左的。
105. 从前序与中序遍历序列构造二叉树
https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
int[] preorder;
int[] inorder;
int pre_index;
HashMap<Integer,Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
this.preorder = preorder;
this.inorder = inorder;
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i],i);
}
pre_index = 0;
return helper(0,inorder.length - 1);
}
public TreeNode helper(int left,int right){
if (left > right) return null;
int concurrentRootVal = preorder[pre_index];
pre_index++;
TreeNode node = new TreeNode(concurrentRootVal);
int index = map.get(concurrentRootVal);
node.left = helper(left,index - 1);
node.right = helper(index + 1 ,right);
return node;
}
总结:力扣官方的代码,递归中每次传中序数组的左右边界,每次递归把当前的root拿出来去给他构造left,right。返回当前root,注意这里要先构造左子树,再右子树,因为前序数组的遍历是先左再右的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?