代码随想录算法训练营第第14天 | 二叉树递归遍历(递归法、迭代、统一的迭代方法)

递归遍历 (必须掌握)

二叉树的三种递归遍历掌握其规律后,其实很简单
题目链接/文章讲解/视频讲解:https://programmercarl.com/二叉树的递归遍历.html

迭代遍历 (基础不好的录友,迭代法可以放过)
题目链接/文章讲解/视频讲解:https://programmercarl.com/二叉树的迭代遍历.html

统一迭代 (基础不好的录友,迭代法可以放过)
这是统一迭代法的写法, 如果学有余力,可以掌握一下
题目链接/文章讲解:https://programmercarl.com/二叉树的统一迭代法.html

  1. 二叉树的前序遍历
一、递归写法,主要先理解递归三部曲
function traverse(node,arr) {
    if(node==null) return;
    arr.push(node.val);
    traverse(node.left,arr);
    traverse(node.right,arr);
}

/**
 * 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) {
    if(root==null) return [];
    const res = [];
    traverse(root,res);
    return res;
};

二、迭代写法
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var preorderTraversal = function(root) {
    if (root==null) return [];
    const stack = [root];
    const res = [];
    while(stack.length){
        let cur = stack.pop();
        res.push(cur.val);
        cur.right && stack.push(cur.right);
        cur.left && stack.push(cur.left);
    }
    return res;
};
  1. 二叉树的中序遍历
一、递归写法
function traverse(node,arr) {
    if(node==null) return;
    traverse(node.left,arr);
    arr.push(node.val);
    traverse(node.right,arr);
    
}

/**
 * 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) {
    if(root==null) return [];
    const res = [];
    traverse(root,res);
    return res;
}

二、迭代写法
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function(root) {
    if (root == null) return [];
    const stack = [];
    const res = [];
    let cur = root;
    while(stack.length || cur){
        if(cur){
            stack.push(cur);
            cur = cur.left;
        } else {
            cur = stack.pop();
            res.push(cur.val);
            cur = cur.right;
        }
    }
    return res;
};


三、统一的迭代写法
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function(root) {
    if (root == null) return [];
    const stack = [root];
    const res = [];
    while(stack.length) {
        let node = stack[stack.length-1];
        if (node !== null) {
            stack.pop();
            node.right && stack.push(node.right);
            stack.push(node);
            stack.push(null);
            node.left && stack.push(node.left);
        } else {
            stack.pop();
            let cur = stack.pop();
            res.push(cur.val);
        }
    }
    return res;
};

145

递归写法和中序先序的递归类似,在此不展示了

迭代写法
/**
 * 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) {
    if (root == null) return [];
    const stack = [root];
    const res = [];
    while(stack.length){
        let cur = stack.pop();
        res.push(cur.val);
        cur.left && stack.push(cur.left);
        cur.right && stack.push(cur.right);
    }
    return res.reverse();
};


统一的迭代写法
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var postorderTraversal = function(root) {
    if (root == null) return [];
    const res = [];
    const stack = [root];
    while(stack.length){
        let node = stack[stack.length-1];
        if (node!==null) {
            stack.pop();
            stack.push(node);
            stack.push(null);
            node.right && stack.push(node.right);
            node.left && stack.push(node.left);
        } else {
            stack.pop();
            let cur = stack.pop();
            res.push(cur.val);
        }
    }
    return res;
};
posted @ 2024-05-21 23:09  YuanYF6  阅读(2)  评论(0编辑  收藏  举报