力扣二叉树专题:中序遍历、对称判断与深度计算 📅 2026/8/8 1:26:16 1. 项目概述力扣二叉树专题精讲作为一名长期混迹力扣的Java开发者我发现在二叉树类题目中中序遍历、对称判断和深度计算是面试最高频的考点。这个专题将带大家用Java实现四个经典问题94题二叉树的中序遍历、101题对称二叉树、104题二叉树的最大深度以及121题买卖股票的最佳时机虽然不属于二叉树但常作为附加题出现。提示本文所有代码均经过力扣官方测试用例验证可直接用于面试准备。建议配合力扣热题100清单同步练习。2. 二叉树基础与Java实现2.1 二叉树结构定义在Java中我们通常这样定义二叉树节点类public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val val; this.left left; this.right right; } }2.2 遍历方式对比二叉树有三种基本遍历方式面试中必须掌握其区别前序遍历根→左→右中序遍历左→根→右本文重点后序遍历左→右→根3. 中序遍历的三种实现方式3.1 递归解法最基础class Solution { public ListInteger inorderTraversal(TreeNode root) { ListInteger res new ArrayList(); inorder(root, res); return res; } void inorder(TreeNode root, ListInteger res) { if (root null) return; inorder(root.left, res); // 左 res.add(root.val); // 根 inorder(root.right, res); // 右 } }避坑指南递归解法虽然简单但在面试时可能会被要求分析空间复杂度。最坏情况下树退化为链表空间复杂度为O(n)。3.2 迭代解法面试常考class Solution { public ListInteger inorderTraversal(TreeNode root) { ListInteger res new ArrayList(); DequeTreeNode stack new ArrayDeque(); TreeNode curr root; while (curr ! null || !stack.isEmpty()) { while (curr ! null) { stack.push(curr); curr curr.left; // 左子树入栈 } curr stack.pop(); res.add(curr.val); // 访问根节点 curr curr.right; // 转向右子树 } return res; } }3.3 Morris遍历空间最优class Solution { public ListInteger inorderTraversal(TreeNode root) { ListInteger res new ArrayList(); TreeNode curr root; while (curr ! null) { if (curr.left null) { res.add(curr.val); curr curr.right; } else { TreeNode prev curr.left; while (prev.right ! null prev.right ! curr) { prev prev.right; } if (prev.right null) { prev.right curr; // 建立线索 curr curr.left; } else { prev.right null; // 拆除线索 res.add(curr.val); curr curr.right; } } } return res; } }经验之谈Morris遍历虽然空间复杂度为O(1)但会修改原树结构面试时需提前说明。建议优先掌握迭代解法。4. 对称二叉树判断4.1 递归解法class Solution { public boolean isSymmetric(TreeNode root) { return root null || checkSymmetric(root.left, root.right); } boolean checkSymmetric(TreeNode left, TreeNode right) { if (left null right null) return true; if (left null || right null) return false; return left.val right.val checkSymmetric(left.left, right.right) checkSymmetric(left.right, right.left); } }4.2 迭代解法队列实现class Solution { public boolean isSymmetric(TreeNode root) { if (root null) return true; QueueTreeNode queue new LinkedList(); queue.offer(root.left); queue.offer(root.right); while (!queue.isEmpty()) { TreeNode left queue.poll(); TreeNode right queue.poll(); if (left null right null) continue; if (left null || right null || left.val ! right.val) { return false; } // 注意入队顺序 queue.offer(left.left); queue.offer(right.right); queue.offer(left.right); queue.offer(right.left); } return true; } }常见错误迭代解法中容易忽略入队顺序必须保证镜像对称的节点成对比较。5. 二叉树的最大深度5.1 递归解法自顶向下class Solution { public int maxDepth(TreeNode root) { if (root null) return 0; return 1 Math.max(maxDepth(root.left), maxDepth(root.right)); } }5.2 迭代解法层序遍历class Solution { public int maxDepth(TreeNode root) { if (root null) return 0; QueueTreeNode queue new LinkedList(); queue.offer(root); int depth 0; while (!queue.isEmpty()) { int size queue.size(); while (size-- 0) { TreeNode node queue.poll(); if (node.left ! null) queue.offer(node.left); if (node.right ! null) queue.offer(node.right); } depth; } return depth; } }性能对比递归解法代码简洁但可能栈溢出层序遍历更适合深度较大的树。6. 买卖股票的最佳时机虽然不属于二叉树题目但作为常见附加题class Solution { public int maxProfit(int[] prices) { int minPrice Integer.MAX_VALUE; int maxProfit 0; for (int price : prices) { if (price minPrice) { minPrice price; } else if (price - minPrice maxProfit) { maxProfit price - minPrice; } } return maxProfit; } }7. 二叉树题目通用解题技巧7.1 递归三要素终止条件通常是节点为null当前层处理逻辑递归调用左右子树7.2 迭代解法模板使用栈实现DFS使用队列实现BFS需要显式维护数据结构7.3 调试技巧打印树结构void printTree(TreeNode root, String prefix) { if (root null) return; System.out.println(prefix root.val); printTree(root.left, prefix L-); printTree(root.right, prefix R-); }8. 面试常见问题与回答思路Q如何判断二叉树是否平衡 A可以在计算深度时同时判断左右子树高度差是否超过1Q二叉树序列化和反序列化怎么做 A通常使用前序遍历配合特殊符号表示空节点Q递归解法有什么缺点 A栈空间有限深度过大会栈溢出存在重复计算问题我在刷题过程中发现二叉树类题目往往有多种解法。建议先从递归入手理解问题本质再掌握迭代写法应对面试最后挑战Morris遍历等优化解法。每道题至少手写3遍直到能无bug一次通过。