算法面试——二叉树遍历:递归、非递归、重建二叉树 二叉树遍历是算法面试的绝对基础。递归写法要熟练非递归写法要能手撕。一、前序遍历voidpreorder(TreeNoderoot){if(rootnull)return;System.out.print(root.val );preorder(root.left);preorder(root.right);}voidpreorderIter(TreeNoderoot){StackTreeNodestacknewStack();if(root!null)stack.push(root);while(!stack.isEmpty()){TreeNodenodestack.pop();System.out.print(node.val );if(node.right!null)stack.push(node.right);if(node.left!null)stack.push(node.left);}}二、中序遍历voidinorderIter(TreeNoderoot){StackTreeNodestacknewStack();TreeNodecurrroot;while(curr!null||!stack.isEmpty()){while(curr!null){stack.push(curr);currcurr.left;}currstack.pop();System.out.print(curr.val );currcurr.right;}}三、后序遍历最难的非递归ListIntegerpostorderIter(TreeNoderoot){LinkedListIntegerresultnewLinkedList();StackTreeNodestacknewStack();if(root!null)stack.push(root);while(!stack.isEmpty()){TreeNodenodestack.pop();result.addFirst(node.val);// 头插法相当于逆序if(node.left!null)stack.push(node.left);if(node.right!null)stack.push(node.right);}returnresult;// 输出顺序左右根}四、层序遍历ListListIntegerlevelOrder(TreeNoderoot){ListListIntegerresultnewArrayList();QueueTreeNodequeuenewLinkedList();queue.offer(root);while(!queue.isEmpty()){intsizequeue.size();ListIntegerlevelnewArrayList();for(inti0;isize;i){TreeNodenodequeue.poll();level.add(node.val);if(node.left!null)queue.offer(node.left);if(node.right!null)queue.offer(node.right);}result.add(level);}returnresult;}五、重建二叉树publicTreeNodebuildTree(int[]preorder,int[]inorder){returnbuild(preorder,0,inorder,0,inorder.length-1);}privateTreeNodebuild(int[]pre,intpreStart,int[]in,intinStart,intinEnd){if(preStartpre.length||inStartinEnd)returnnull;TreeNoderootnewTreeNode(pre[preStart]);introotIdxinStart;while(in[rootIdx]!root.val)rootIdx;intleftLenrootIdx-inStart;root.leftbuild(pre,preStart1,in,inStart,rootIdx-1);root.rightbuild(pre,preStartleftLen1,in,rootIdx1,inEnd);returnroot;} 觉得有用的话点赞 关注【张老师技术栈】吧每周更新 Java/Python/爬虫 实战干货不让你白来。

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/9/25 12:45:43
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/24 14:25:52
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/24 14:49:33
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/9/23 8:01:38
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/24 14:28:18
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/25 15:49:36

日新闻

周新闻