5.16 二轮 链表

This commit is contained in:
zhangsan 2025-05-16 14:49:54 +08:00
parent 4acf3b868b
commit 15f9ccde14
7 changed files with 211 additions and 0 deletions

View File

@ -1,5 +1,22 @@
package linkedlist; package linkedlist;
/**
* 题目 146. LRU 缓存 (LRUCache)
* 描述请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构
* 实现 LRUCache
* LRUCache(int capacity) 正整数 作为容量 capacity 初始化 LRU 缓存
* int get(int key) 如果关键字 key 存在于缓存中则返回关键字的值否则返回 -1
* void put(int key, int value) 如果关键字 key 已经存在则变更其数据值 value 如果不存在则向缓存中插入该组 key-value 如果插入操作导致关键字数量超过 capacity 则应该 逐出 最久未使用的关键字
* 函数 get put 必须以 O(1) 的平均时间复杂度运行
示例 1
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]
* 链接https://leetcode.cn/problems/lru-cache/
*/
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -0,0 +1,34 @@
package tree;
/**
* 题目 106. 从中序与后序遍历序列构造二叉树 (buildTree)
* 描述给定两个整数数组 inorder postorder 其中 inorder 是二叉树的中序遍历 postorder 是同一棵树的后序遍历请你构造并返回这颗 二叉树
示例 2
输入inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出[3,9,20,null,null,15,7]
* 链接https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
*/
public class BuildTree2 {
int find_pos(Integer num,int[] inorder,int i,int j){
for (int k = i;k <= j; k++) {
if(num==inorder[k])
return k;
}
return 0;
}
TreeNode func(int[] inorder, int[] postorder,int l_in,int r_in,int l_post,int r_post){
if(l_in>r_in||l_post>r_post)
return null;
int cur=postorder[r_post];
int pos=find_pos(cur,inorder,l_in,r_in);
int l_cnt=pos-l_in;
TreeNode node=new TreeNode(cur);
node.left=func(inorder,postorder,l_in,pos-1,l_post,l_post+l_cnt-1);
node.right=func(inorder,postorder,pos+1,r_in,l_post+l_cnt,r_post-1);
return node;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
return func(inorder,postorder,0,inorder.length-1,0,postorder.length-1);
}
}

View File

@ -0,0 +1,55 @@
package tree;
import java.util.LinkedList;
import java.util.Queue;
/**
* 题目 117. 填充每个节点的下一个右侧节点指针 II (buildTree)
* 描述给定两个整数数组 inorder postorder 其中 inorder 是二叉树的中序遍历 postorder 是同一棵树的后序遍历请你构造并返回这颗 二叉树
示例 2
输入inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出[3,9,20,null,null,15,7]
* 链接https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
*/
public class Connect {
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
public Node connect(Node root) {
Queue<Node>queue=new LinkedList<>();
if(root==null)return null;
queue.offer(root);
while (!queue.isEmpty()){
int sz=queue.size();
Node cur=queue.poll();
if(cur.left!=null)queue.offer(cur.left);
if(cur.right!=null)queue.offer(cur.right);
for (int i = 1; i < sz; i++) {
Node next =queue.poll();
cur.next=next;
cur=next;
if(cur.left!=null)queue.offer(cur.left);
if(cur.right!=null)queue.offer(cur.right);
}
}
return root;
}
}

View File

@ -0,0 +1,26 @@
package tree;
/**
* 题目 112. 路径总和 (buildTree)
* 描述给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 判断该树中是否存在 根节点到叶子节点 的路径这条路径上所有节点值相加等于目标和 targetSum 如果存在返回 true 否则返回 false
* 叶子节点 是指没有子节点的节点
*
示例 1
输入root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出true
解释等于目标和的根节点到叶节点路径如上图所示
* 链接https://leetcode.cn/problems/path-sum/
*/
public class HasPathSum {
boolean dfs(TreeNode root,int curSum,int target){
if(root==null)return false;
curSum+=root.val;
if(root.right==null&&root.left==null){
if(curSum==target)return true;
}
return dfs(root.left,curSum,target) || dfs(root.right,curSum,target);
}
public boolean hasPathSum(TreeNode root, int targetSum) {
return dfs(root,0,targetSum);
}
}

View File

@ -0,0 +1,26 @@
package tree;
/**
* 题目 100. 相同的树 (isSameTree)
* 描述给你两棵二叉树的根节点 p q 编写一个函数来检验这两棵树是否相同
* 如果两个树在结构上相同并且节点具有相同的值则认为它们是相同的
示例 2
输入p = [1,2,3], q = [1,2,3]
输出true
* 链接https://leetcode.cn/problems/same-tree/
*/
public class IsSameTree {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} else if (p == null || q == null) {
return false;
} else if (p.val != q.val) {
return false;
} else {
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
}

View File

@ -0,0 +1,37 @@
package tree;
/**
* 题目 129. 求根节点到叶节点数字之和 (sumNumbers)
* 描述给你一个二叉树的根节点 root 树中每个节点都存放有一个 0 9 之间的数字
* 每条从根节点到叶节点的路径都代表一个数字
* 例如从根节点到叶节点的路径 1 -> 2 -> 3 表示数字 123
* 计算从根节点到叶节点生成的 所有数字之和
*
* 叶节点 是指没有子节点的节点
*
示例 1
输入root = [1,2,3]
输出25
解释
从根到叶子节点路径 1->2 代表数字 12
从根到叶子节点路径 1->3 代表数字 13
因此数字总和 = 12 + 13 = 25
* 链接https://leetcode.cn/problems/path-sum/
*/
public class SumNumbers {
int dfs(TreeNode root,int curSum){
int total=0;
curSum=curSum*10+root.val;
if(root.left==null&&root.right==null)
return curSum;
if(root.left!=null)
total+=dfs(root.left,curSum);
if(root.right!=null)
total+=dfs(root.right,curSum);
return total;
}
public int sumNumbers(TreeNode root) {
return dfs(root,0);
}
}

View File

@ -0,0 +1,16 @@
package tree;
import org.junit.Test;
import static org.junit.Assert.*;
public class BuildTree2Test {
@Test
public void buildTree() {
int[] inorder = {9,3,15,20,7}, postorder = {9,15,7,20,3};
BuildTree2 solution = new BuildTree2();
solution.buildTree(inorder,postorder);
}
}