news 2026/4/15 17:54:25

力扣二叉树的三种遍历

作者头像

张小明

前端开发工程师

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/4/16 10:59:11

Phaser游戏引擎中智能跟随系统的技术实现深度解析

Phaser游戏引擎中智能跟随系统的技术实现深度解析 【免费下载链接】phaser Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering. 项目地址: https://gitcode.com/gh_mir…

作者头像 李华
网站建设 2026/4/16 14:23:07

AI编程工具Cursor发布可视化编辑器: AI自动生成代码

Cursor 推出浏览器可视化编辑器,支持拖拽布局、自然语言修改、组件状态调试,实现设计与代码无缝融合,开启 AI 驱动的下一代前端开发范式。 Cursor 浏览器推出可视化编辑器:设计与代码从未如此无缝融合 AI 编程工具 Cursor 正式发…

作者头像 李华
网站建设 2026/4/16 13:30:52

基于SpringBoot实现的云宠之家管理系统设计与实现

基于SpringBoot实现的云宠之家管理系统设计与实现 一、系统开发背景与核心价值 随着宠物经济的蓬勃发展,宠物饲养家庭对专业化、便捷化的宠物服务需求日益迫切。传统宠物管理模式存在信息分散、服务流程不规范、用户交互不便等问题,如宠物健康档案混乱、…

作者头像 李华
网站建设 2026/4/16 12:27:01

智能阅读推荐系统

智能阅读推荐系统设计与实现 一、系统开发背景与核心价值 在信息爆炸的数字化时代,海量图书、文章等阅读资源让用户面临“选择困境”,传统阅读平台的推荐模式多依赖人工分类或热门排行,难以精准匹配用户个性化需求。用户往往花费大量时间筛选…

作者头像 李华
网站建设 2026/4/15 17:41:19

IT66122:低功率HDMI 1.4 4K2Kp30发射器

该IT66122是一款高性能、低功耗单通道HDMI发射机,完全符合HDMI 1.3a、HDCP 1.2标准,并向下兼容DVI 1.0规范。IT66122还提供HDMI 1.4 3D功能,通过HDMI链路实现直接3D显示。该IT66122旨在为数字电视兼容的消费电子产品(如机顶盒、DV…

作者头像 李华