力扣二叉树的三种遍历

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

一、前序

两种方法:

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;
};
相关推荐
超级码力6662 小时前
【Latex文件架构】Latex文件架构模板
算法·数学建模·信息可视化
穿条秋裤到处跑2 小时前
每日一道leetcode(2026.04.29):二维网格图中探测环
算法·leetcode·职场和发展
matlab_xiaowang2 小时前
Redux 入门:JavaScript 可预测状态管理库
开发语言·javascript·其他·ecmascript
Merlos_wind3 小时前
HashMap详解
算法·哈希算法·散列表
汉克老师3 小时前
GESP2025年3月认证C++五级( 第三部分编程题(1、平均分配))
c++·算法·贪心算法·排序·gesp5级·gesp五级
前端摸鱼匠4 小时前
Vue 3 的v-bind合并行为:讲解v-bind与普通属性合并的规则
前端·javascript·vue.js·前端框架·ecmascript
REDcker5 小时前
浏览器端Web程序性能分析与优化实战 DevTools指标与工程清单
开发语言·前端·javascript·vue·ecmascript·php·js
Yzzz-F5 小时前
Problem - 2205D - Codeforces
算法
智者知已应修善业6 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
Halo_tjn6 小时前
Java Set集合相关知识点
java·开发语言·算法