力扣二叉树的三种遍历

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

一、前序

两种方法:

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;
};
相关推荐
可编程芯片开发11 小时前
基于PI控制的三相整流器控制系统的simulink建模与仿真,包含超级电容充电和电机
算法
JackieZhengChina11 小时前
零依赖轻量级JS悬浮提示插件title2tip-js的使用教程
开发语言·javascript·ecmascript
酿情师11 小时前
区块链共识算法深度拆解:PoW、PoS、PBFT、Raft 原理解析
算法·区块链·共识算法
hansang_IR11 小时前
【记录】「SCOI2016」三道模拟赛/26.7.12
c++·算法
大家的林语冰11 小时前
👍 Rust 为 Node 插上翅膀,反超 Bun 和 pnpm,一体化工具我也有!
前端·javascript·node.js
退休倒计时12 小时前
【每日一题】LeetCode 39. 组合总和 TypeScript
算法·leetcode·typescript
不是az12 小时前
力控相关知识点
算法·机器学习·最小二乘法
ssshooter12 小时前
Promise 和 async/await 被 try-catch 包裹时的行为差异
前端·javascript·面试
战血石LoveYY12 小时前
Integer类型超限,导致数据异常
java·算法
紫金修道12 小时前
PnP算法介绍(Perspective-n-Point)
算法