力扣二叉树的三种遍历

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

一、前序

两种方法:

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;
};
相关推荐
袋鼠云数栈UED团队21 小时前
基于 Lexical 实现变量输入编辑器
前端·javascript·架构
亦妤1 天前
JS执行机制、作用域及作用域链
javascript
SuperEugene1 天前
表单最佳实践:从 v-model 到自定义表单组件(含校验)
前端·javascript·vue.js
不会敲代码11 天前
React性能优化:深入理解useMemo和useCallback
前端·javascript·react.js
YukiMori231 天前
一个有趣的原型继承实验:为什么“男人也会生孩子”?从对象赋值到构造函数继承的完整推演
前端·javascript
摸鱼的春哥1 天前
惊!黑客靠AI把墨西哥政府打穿了,海量数据被黑
前端·javascript·后端
小兵张健1 天前
Playwright MCP 截图标注方案调研(推荐方案1)
前端·javascript·github
我叫黑大帅1 天前
Vue3和Uniapp的爱恨情仇:小白也能懂的跨端秘籍
前端·javascript·vue.js
None3211 天前
【NestJs】使用Winston+ELK分布式链路追踪日志采集
javascript·node.js
Qinana1 天前
从代码到智能体:MCP 协议如何重塑 AI Agent 的边界
前端·javascript·mcp