力扣二叉树的三种遍历

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

一、前序

两种方法:

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;
};
相关推荐
ting945200011 小时前
Humalike X Hermes 深度技术剖析:单指令注入群聊社交智能的底层架构、算法与跨 IM 平台实现
人工智能·算法·架构
吴声子夜歌11 小时前
Java面试——算法
java·算法·面试
evans在进步11 小时前
LeetCode 64:最小路径和——Java 原地动态规划详解
java·leetcode·动态规划
h_a_o777oah12 小时前
【图论】Tarjan 缩点:解决有向图中环的问题
c++·算法·图论·acm·强连通分量·缩点·tarjan
Tisfy12 小时前
LeetCode 1386.安排电影院座位:哈希表+位运算
算法·leetcode·散列表·题解·哈希表
Nil20812 小时前
leetcode 199二叉树的右视图
算法·leetcode·深度优先
M78佐菲13 小时前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法
AI备案指南-满满15 小时前
人工智能拟人化互动服务安全自评估报告的评估要点有哪些?
人工智能·算法·安全·机器人·大模型备案·算法备案