力扣二叉树的三种遍历

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

一、前序

两种方法:

1.递归

javascript 复制代码
/**
 * 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.迭代

javascript 复制代码
/**
 * 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.递归

javascript 复制代码
/**
 * 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.迭代

javascript 复制代码
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.递归

javascript 复制代码
/**
 * 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.迭代

javascript 复制代码
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;
};
相关推荐
Zane19941 分钟前
冒泡、选择、插入排序都是O(n²),希尔排序凭什么说自己能更快
算法·排序算法
梦帮科技20 分钟前
Next.js 16 模块化单体实战:App Router、领域模块与 Route Handler 分层
css·数据结构·链表·正则表达式·node.js·json·html5
mONESY1 小时前
从 RAG 到 Agentic RAG:固定流程的五个毛病,和把它拆成一张图
javascript
余额瞒着我当琳1 小时前
C++多态深入解析:多态概念,多态实现条件,虚函数表与内存分布,常见问题及注意事项
c++·算法
梦帮科技1 小时前
从 Guest 到 Platinum:Prompt 配额、API Key 与访问控制的安全闭环
javascript·git·架构·node.js·reactjs·html5·visual studio
雨落在了我的手上1 小时前
Java数据结构(十三):排序
数据结构
linux_cfan1 小时前
videojs v10 源代码系列解读:10 · `DestroyMixin`:双重 rAF 延迟销毁
前端·javascript·音视频
维克兜率天1 小时前
【维克】模块3总结:从一行空数据,到一个能跑的模型
python·深度学习·算法
飞Link1 小时前
定积分理论与 Python 仿真完全指南
python·算法