news 2026/5/1 5:25:49

力扣二叉树的三种遍历

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
力扣二叉树的三种遍历

这篇文章类比三种遍历的写法,每个遍历利用两种方法,分别是递归和迭代,帮助更好的学习二叉树的相关知识。

一、前序

两种方法:

1.递归

/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @return {number[]} */ var preorderTraversal = function(root) { const res = []; const preorder = (node) => { if(node === null) return; //先搜集根节点 res.push(node.val); //递归遍历左子树 preorder(node.left); //递归遍历右子树 preorder(node.right) } preorder(root); return res; };

2.迭代

/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @return {number[]} */ var preorderTraversal = function(root) { const res = []; // 存储遍历结果 const stk = []; // 模拟递归的栈 // 循环条件和中序/后序一致:当前节点非空 或 栈非空 while (root || stk.length) { while (root) { res.push(root.val); stk.push(root); root = root.left; } root = stk.pop(); root = root.right; } return res; };

二、中序

两种方法:

1.递归

/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @return {number[]} */ var inorderTraversal = function(root) { const res = []; const inorder = (node) => { if (node === null) return; // 先递归遍历左子树 inorder(node.left); // 收集当前节点值 res.push(node.val); // 再递归遍历右子树 inorder(node.right); }; inorder(root); return res; };

2.迭代

var inorderTraversal = function(root) { const res = []; const stk = []; while (root || stk.length) { while (root) { stk.push(root); root = root.left; } root = stk.pop(); res.push(root.val); root = root.right; } return res; };

三、后序

两种方法:

1.递归

/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @return {number[]} */ var postorderTraversal = function(root) { const res = []; const postorder = (node) => { if(node === null) return; //递归遍历左子树 postorder(node.left); //递归遍历右子树 postorder(node.right); //先搜集根节点 res.push(node.val); } postorder(root); return res; };

2.迭代

var postorderTraversal = function(root) { const res = []; const stk = []; let prev = null; while (root || stk.length) { while (root) { stk.push(root); root = root.left; } root = stk.pop(); if (!root.right || root.right === prev) { res.push(root.val); prev = root; root = null; } else { stk.push(root); root = root.right; } } return res; };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 5:24:12

29、优化Azure应用程序扩展与静态内容分发

优化Azure应用程序扩展与静态内容分发 1. Azure应用程序扩展 在Azure中,有多种服务的扩展方式值得关注,包括Azure App Service、Azure Functions和Azure Service Fabric。 1.1 Azure Function扩展控制 对于Azure Function App,你可以通过在应用设置中提供 WEBSITE_MAX_…

作者头像 李华
网站建设 2026/5/1 5:24:34

C / C++ 调用 DLL 的几种常见方法

C / C 调用 DLL 的几种常见方法(详解 示例) 在 Windows 平台开发中,DLL(Dynamic Link Library,动态链接库) 是非常核心的一种代码复用方式。 无论是: 调用第三方 SDK拆分大型工程插件化设计…

作者头像 李华
网站建设 2026/5/1 5:23:25

基于SSM的一站式酒店管理系统

基于SSM的一站式酒店管理系统设计与实现 一、系统开发背景与核心价值 随着旅游业与商务出行的持续升温,酒店行业迎来规模化发展的同时,也面临着管理效率低、服务流程不规范、客户体验参差不齐等问题。传统酒店管理模式依赖人工记录订单、统计房源、核对账…

作者头像 李华
网站建设 2026/4/30 4:32:38

基于WEB的大学生心理互助社区

基于WEB的大学生心理互助社区设计与实现 一、社区开发背景与核心价值 当前大学生面临学业压力、人际关系、就业焦虑等多重心理挑战,传统心理支持模式存在资源有限、求助门槛高、隐私保护不足等问题。线下心理咨询预约难、咨询时段固定,部分学生因顾虑隐私…

作者头像 李华
网站建设 2026/4/23 15:45:57

FastAPI 入门指南

FastAPI 是近年来 Python 生态中增长最快的 Web 框架之一,因其高性能、强类型、自动化文档、优秀的异步支持,已成为构建 API 服务、AI 推理接口、数据服务的主流选择。 本文将从纯后端技术视角系统介绍 FastAPI 的核心能力、设计理念与工程化实践&#…

作者头像 李华
网站建设 2026/4/22 21:42:54

LangChain vs Dify:大模型应用开发工具选择指南,看完就会用!

简介 LangChain是面向开发者的开源代码框架,提供高度灵活的定制能力;Dify是低代码平台,通过可视化操作实现快速应用开发。LangChain适合复杂定制和高性能需求场景,Dify适合快速落地和非技术团队使用。二者结合可实现核心定制与便捷…

作者头像 李华