当前位置: 首页 > news >正文

力扣二叉树的三种遍历

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

一、前序

两种方法:

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; };
http://www.cnnetsun.cn/news/61931.html

相关文章:

  • OptiScaler游戏画质优化工具深度解析
  • 16、Yocto项目开发工具与流程详解
  • 25、深入解析Linux相关技术:从CGL到汽车级Linux
  • Nature同款 | 跟着顶刊学配色第 26 期
  • Gin框架架构详解:高性能Go语言Web框架的设计哲学与实践
  • 【OpenHarmony】轻量级公共基础库commonlibrary_utils_lite
  • 41、Linux系统深入解析与操作指南
  • SSM小型餐饮综合管理系统j1c7m(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
  • 2025年计算机类专业的就业分析
  • 社区工作者资源合集(第二辑)
  • 护网怎么做,护网前、护网中,护网后,总共60道工序,一道一道
  • 远程管理效能革命:Quasar架构下的智能传输体系重构
  • Happy LLM:Github爆火!手把手教你从0手搓个大模型!
  • SSM线上学习系统8e88w(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
  • 深度解析:MindsDB与ChromaDB向量数据库集成的高效实战指南
  • 32、深入了解Samba与Linux安全策略
  • 26、调试 Shell 程序的实用方法
  • Symbolic 英文单词学习
  • AI开发全流程工具链:从编码辅助到模型部署的实战指南
  • 英语综合练习题
  • 电力物联网系统能够发挥什么作用
  • 压气站SCADA数据采集远程监控系统方案
  • 12、高级渗透测试与中间人攻击技术详解
  • Vue3 生命周期全面解析:从创建到销毁的完整指南
  • 3个让我后悔的StyleGAN2数据集错误:从失败到成功的真实经历
  • 电商数据采集 API 接口:全流程采集与分析指南(附实战代码)
  • 7、Docker 镜像构建、注册与存储全解析
  • Python语法基础笔记(四)
  • 13、找回丢失文件的实用方法
  • 14、Linux 用户与用户组管理全解析