【LeetCode】 JS实现 二叉树(前、中、后、层序)遍历(递归、迭代法)

144. 二叉树的前序遍历

原题链接: 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) {
    // 递归
    let res=[];
    const preOrder=(root)=>{
        if(!root){
            return
        }else{
            res.push(root.val);
            preOrder(root.left);
            preOrder(root.right);
        }
    }
    preOrder(root);
    return res;
};

迭代法: 用的是push()方法

var preorderTraversal = function(root) {
    let res=[];
    if(!root) return res;
    const stack=[];
    // 根节点入栈
    stack.push(root);
    while(stack.length){
        const cur=stack.pop();
        res.push(cur.val);
        if(cur.right){
            stack.push(cur.right);
        }
        if(cur.left){
            stack.push(cur.left);
        }
    }
    return res;
};

145. 二叉树的后序遍历

原题链接: 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=[];
    const postOrder=(root)=>{
        if(!root){
            return
        }else{
            postOrder(root.left);
            postOrder(root.right);
            res.push(root.val);
        }
    }
    postOrder(root);
    return res;
};

迭代法: 用的是unshift()方法

var postorderTraversal = function(root) {
    let res=[];
    if(!root) return res;
    const stack=[];
    // 根节点入栈
    stack.push(root);
    while(stack.length){
        const cur=stack.pop();
        res.unshift(cur.val);
        if(cur.left){
            stack.push(cur.left);
        }
        if(cur.right){
            stack.push(cur.right);
        }
    }
    return res;
};

94. 二叉树的中序遍历

原题链接: 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) {
    // 递归
    let res=[];
    const inorder=(root)=>{
        if(!root){
             return
        }else{
            inorder(root.left);
            res.push(root.val);
            inorder(root.right);
        }
    }
    inorder(root);
    return res;
};

迭代法:

var inorderTraversal = function(root) {
    let res=[];
    const stack=[];
    // cur用于游标来不断向下搜索
    let cur=root;
    while(cur||stack.length){
        // 寻找最左叶子结点,途径的结点全保存下来
        while(cur){
            //将途经的结点入栈
            stack.push(cur);
            // 搜索左孩子
            cur=cur.left;
        }
        cur=stack.pop();
        res.push(cur.val);
        // 搜索右孩子
        cur=cur.right;
    }
    return res;
};

102. 二叉树的层序遍历

原题链接: 102. 二叉树的层序遍历

在这里插入图片描述

递归法:

/**
 * 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 levelOrder = function(root) {
    //用一个变量记录当前所在的层 数组的下标为当前所在的层
    const arr=[];
    const order=(node,h)=>{
        if(!node) return;
        // 如果第一次进入该层 创建空数组 
        if(!arr[h]) arr[h]=[];
        // 将节点值添加到该层数组
        arr[h].push(node.val);

        order(node.left,h+1);
        order(node.right,h+1);
    }
    order(root,0);
    return arr;
};

迭代法:

var levelOrder = function(root) {
    //迭代法
    let res=[],queue=[];
    queue.push(root);
    if(root===null){
        return res;
    }
    while(queue.length!==0){
        // 记录当前层级节点数
        let length=queue.length;
        //存放每一层的节点 
        let curLevel=[];
         //遍历当前层级的结果
        for(let i=0;i<length;i++){
            // 将当前队列第一个节点移出 赋值给node
            let node=queue.shift();
            curLevel.push(node.val);
            // 存放当前层下一层的节点
            if(node.left) queue.push(node.left);
            if(node.right) queue.push(node.right);
        }
        //把每一层的结果放到结果数组
        res.push(curLevel);
    }
    return res;
};

107. 二叉树的层序遍历 II

原题链接: 107. 二叉树的层序遍历 II

在这里插入图片描述

迭代法:

/**
 * 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 levelOrderBottom = function(root) {
    let res=[],queue=[];
    queue.push(root);
    if(!root) return res;

    while(queue.length!==0){
        let len=queue.length;
        let curArr=[];
        for(let i=0;i<len;i++){
            let node=queue.shift();
            curArr.push(node.val);
            if(node.left) queue.push(node.left);
            if(node.right) queue.push(node.right);
        }
        //在存入res结果数组时 注意存入顺序
        //res.push(curArr);
        res.unshift(curArr);
    }
    // return res.reverse();
    return res;
};
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值