代码随想录算法训练营第十四天|● 理论基础 ● 递归遍历 ● 迭代遍历 ● 统一迭代(JS写法)

二叉树的前、中、后序遍历

题目链接/文章讲解/视频讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html

144 二叉树的前序遍历

方法一:递归法

/**
 * 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 = function(root){
        if(root == null) return null;
        res.push(root.val);
        preOrder(root.left);
        preOrder(root.right);
    };
    preOrder(root);
    return res;

};

方法二:迭代法

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

注意:
1.这里cur使用的是pop,而不是shift,这里要与层序遍历区分开,层序遍历是用队列来模拟的,因此使用的是shift,取第一个元素;前序遍历是用栈来模拟的,因此使用的是pop。
2.因为使用的是栈,所以要按照中右左的顺序压入栈,这样出栈的时候才是中左右。

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) {
    let res = [];
    let postOrder = function(root){
        if(root == null) return null;
        postOrder(root.left);
        postOrder(root.right);
        res.push(root.val);
    };
    postOrder(root);
    return res;
};

方法二:迭代

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

注意
1.后序的迭代和前序的迭代很像,但是也有一些细节改动。
前序的迭代遍历是先push右再push左,正常是中左右(中不用考虑,因为中会先pop出来),由于是栈结构,所以需要先push右再push左,这样出栈顺序就是中左右了。
同理,后序遍历正常是左右中,因此先push左再push右,出栈顺序就是中右左,最后再reverse数组,就是左右中了。

94 二叉树的中序遍历

方法一:递归

/**
 * 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 = function(root){
        if(root == null) return null;
        inOrder(root.left);
        res.push(root.val);
        inOrder(root.right);
    };
    inOrder(root);
    return res;

};

方法二:迭代
在这里插入图片描述

/**
 * 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) {
    //虽然是左中右,但是首先访问的还是根节点,只不过不是首先处理根节点
    let res = [];
    let stack = [];
    while(root){
        stack.push(root);
        root = root.left;
    }
    while(stack.length){
        let cur = stack.pop();
        res.push(cur.val);
        cur = cur.right;
        while(cur){
            stack.push(cur);
            cur = cur.left;
        }
    }
    return res;
};
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值