大厂算法面试 7 天冲刺:第6天-树与图深度剖析——高频算法面试题 & Java 实战

🧠 第6天:树与图深度剖析------高频算法面试题 & Java 实战


📚 一、核心知识概览 Overview

1. 树(Tree)

树是一种非线性数据结构,常见于面试中的二叉树(Binary Tree)、二叉搜索树(BST)、N叉树等。

常见面试考点:

  • 树的遍历(前序、中序、后序、层序)
  • 最近公共祖先(Lowest Common Ancestor, LCA)
  • 判断平衡树、对称树、二叉搜索树验证等

2. 图(Graph)

图是一种更复杂的数据结构,常用于建模复杂关系,如社交网络、地图、网络延迟等。

常见面试考点:

  • 图的表示(邻接表 / 邻接矩阵)
  • BFS / DFS 遍历
  • 拓扑排序、最短路径(Dijkstra、Floyd)、网络延迟

🧩 二、算法实战 Algorithms & Java 实现


🌟 案例1:二叉树的层序遍历(Level Order Traversal)

💬 题目描述

给你一棵二叉树,请你返回其按层序遍历得到的节点值。

✅ 示例输入
java 复制代码
Input: [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
🔍 解法:BFS(队列实现)
java 复制代码
public List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> result = new ArrayList<>();
    if (root == null) return result;
    
    Queue<TreeNode> queue = new LinkedList<>();
    queue.offer(root);
    
    while (!queue.isEmpty()) {
        int size = queue.size();
        List<Integer> level = new ArrayList<>();
        
        for (int i = 0; i < size; i++) {
            TreeNode node = queue.poll();
            level.add(node.val);
            if (node.left != null) queue.offer(node.left);
            if (node.right != null) queue.offer(node.right);
        }
        result.add(level);
    }
    return result;
}

🌟 案例2:二叉树的最近公共祖先(Lowest Common Ancestor)

💬 题目描述

给定一棵二叉树,找到两个节点的最近公共祖先。

✅ 示例输入
java 复制代码
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1  
Output: 3
🔍 解法:递归 + 后序遍历
java 复制代码
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    if (root == null || root == p || root == q) return root;
    
    TreeNode left = lowestCommonAncestor(root.left, p, q);
    TreeNode right = lowestCommonAncestor(root.right, p, q);
    
    if (left != null && right != null) return root;
    return left != null ? left : right;
}

🌟 案例3:网络延迟时间(Network Delay Time)

💬 题目描述

给定一个有向图,每条边由三元组 (u, v, w) 表示从节点 uv 的路径时间为 w,返回从源节点 K 发出信号,到所有节点的最短时间

✅ 示例输入
java 复制代码
Input: times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2  
Output: 2
🔍 解法:Dijkstra 算法(优先队列实现最短路径)
java 复制代码
public int networkDelayTime(int[][] times, int N, int K) {
    Map<Integer, List<int[]>> graph = new HashMap<>();
    for (int[] time : times) {
        graph.computeIfAbsent(time[0], k -> new ArrayList<>()).add(new int[]{time[1], time[2]});
    }

    int[] dist = new int[N + 1];
    Arrays.fill(dist, Integer.MAX_VALUE);
    dist[K] = 0;

    PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
    pq.offer(new int[]{K, 0});

    while (!pq.isEmpty()) {
        int[] cur = pq.poll();
        int u = cur[0], time = cur[1];
        if (time > dist[u]) continue;
        if (!graph.containsKey(u)) continue;
        for (int[] next : graph.get(u)) {
            int v = next[0], w = next[1];
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                pq.offer(new int[]{v, dist[v]});
            }
        }
    }

    int max = Arrays.stream(dist).skip(1).max().getAsInt();
    return max == Integer.MAX_VALUE ? -1 : max;
}

🛠 三、JDK 与框架中的应用

数据结构 应用场景 框架示例
Tree TreeMap、TreeSet、BinaryTree 实现 Java Collection Framework
Graph 服务依赖图、任务拓扑排序 Spring Boot AutoConfig、微服务拓扑

📌 四、总结 Summary

  • 树结构用于分层表达、层级遍历、祖先查找等问题。
  • 图结构用于最短路径、传递闭包、依赖分析等问题。
  • 常用算法包括 BFS/DFS/Dijkstra/拓扑排序 等,掌握 Java 实现是通关面试的关键!
相关推荐
NAGNIP4 小时前
万字长文!回归模型最全讲解!
算法·面试
之歆4 小时前
Spring AI入门到实战到原理源码-MCP
java·人工智能·spring
知乎的哥廷根数学学派4 小时前
面向可信机械故障诊断的自适应置信度惩罚深度校准算法(Pytorch)
人工智能·pytorch·python·深度学习·算法·机器学习·矩阵
yangminlei4 小时前
Spring Boot3集成LiteFlow!轻松实现业务流程编排
java·spring boot·后端
qq_318121594 小时前
互联网大厂Java面试故事:从Spring Boot到微服务架构的技术挑战与解答
java·spring boot·redis·spring cloud·微服务·面试·内容社区
J_liaty4 小时前
Spring Boot整合Nacos:从入门到精通
java·spring boot·后端·nacos
阿蒙Amon5 小时前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
daidaidaiyu5 小时前
Spring IOC 源码学习 一文学习完整的加载流程
java·spring
666HZ6665 小时前
数据结构2.0 线性表
c语言·数据结构·算法
2***d8855 小时前
SpringBoot 集成 Activiti 7 工作流引擎
java·spring boot·后端