3.手写JavaScript前序中序后序递归遍历二叉树

一、核心思想

1.前序

本质是中左右,对每个结点执行中左右的操作,全部结点执行完毕后,返回结果

2.中序

本质是左中右,对每个结点执行左中右的操作,全部结点执行完毕后,返回结果

3.后序

本质是左右中,对每个结点执行左右中的操作,全部结点执行完毕后,返回结果

ps:注意初始值为空的情况

二、代码实现

1.前序

javascript 复制代码
let tree = {
  val: 1,
  leftChild: {
    val: 2,
    leftChild: {
      val: 3,
      leftChild: {
        val: 4,
      },
      rightChild: {
        val: 5,
      },
    },
    rightChild: {
      val: 6,
    },
  },
  rightChild: {
    val: 7,
    leftChild: {
      val: 8,
    },
    rightChild: {
      val: 9,
      leftChild: {
        val: 10,
      },
    },
  },
};
/**
 * 前序遍历二叉树
 * @param {Array} root 传入数组
 * @return {Array} list 返回深度遍历二叉树数组结果
 */
function preOrder(root){
  let list = []
  if (!root) {
    return []
  }
  function order(root){
    list.push(root.val)
    if (root.leftChild) {
      order(root.leftChild)
    }
    if (root.rightChild) {
      order(root.rightChild)
    }
  }
  order(root)
  return list
}
console.log(preOrder(tree))
// [
//   1, 2, 3, 4,  5,
//   6, 7, 8, 9, 10
// ]

2.中序

javascript 复制代码
let tree = {
  val: 1,
  leftChild: {
    val: 2,
    leftChild: {
      val: 3,
      leftChild: {
        val: 4,
      },
      rightChild: {
        val: 5,
      },
    },
    rightChild: {
      val: 6,
    },
  },
  rightChild: {
    val: 7,
    leftChild: {
      val: 8,
    },
    rightChild: {
      val: 9,
      leftChild: {
        val: 10,
      },
    },
  },
};
/**
 * 中序遍历二叉树
 * @param {Array} root 传入数组
 * @return {Array} list 返回深度遍历二叉树数组结果
 */
function midOrder(root){
  let list = []
  if (!root) {
    return []
  }
  function order(root){
    if (root.leftChild) {
      order(root.leftChild)
    }
    list.push(root.val)
    if (root.rightChild) {
      order(root.rightChild)
    }
  }
  order(root)
  return list
}
console.log(midOrder(tree))
// [
//   4, 3, 5,  2, 6,
//   1, 8, 7, 10, 9
// ]

3.后序

javascript 复制代码
let tree = {
  val: 1,
  leftChild: {
    val: 2,
    leftChild: {
      val: 3,
      leftChild: {
        val: 4,
      },
      rightChild: {
        val: 5,
      },
    },
    rightChild: {
      val: 6,
    },
  },
  rightChild: {
    val: 7,
    leftChild: {
      val: 8,
    },
    rightChild: {
      val: 9,
      leftChild: {
        val: 10,
      },
    },
  },
};
/**
 * 后序遍历二叉树
 * @param {Array} root 传入数组
 * @return {Array} list 返回深度遍历二叉树数组结果
 */
function afterOrder(root){
  let list = []
  if (!root) {
    return []
  }
  function order(root){
    if (root.leftChild) {
      order(root.leftChild)
    }
    if (root.rightChild) {
      order(root.rightChild)
    }
    list.push(root.val)
  }
  order(root)
  return list
}
console.log(afterOrder(tree))
// [
//   4,  5, 3, 6, 2,
//   8, 10, 9, 7, 1
// ]
相关推荐
墨染点香1 分钟前
LeetCode 刷题【135. 分发糖果】
算法·leetcode·职场和发展
Zhu_S W14 分钟前
Redis跳表:高效有序数据结构的深度剖析
数据结构·数据库·redis
秋风战士27 分钟前
通信算法之336 :3GPPMixed Mode Turbo Decoder
算法·matlab·fpga开发·信息与通信·基带工程
是那盏灯塔31 分钟前
【算法】——动态规划之01背包问题
数据结构·c++·算法·动态规划
im_AMBER35 分钟前
Leetcode 41
笔记·学习·算法·leetcode
掘金安东尼1 小时前
Transformers.js:让大模型跑进浏览器
开发语言·javascript·ecmascript
@PHARAOH1 小时前
HOW - localstorage 超时管理方案
前端·javascript·vue.js
jinmo_C++1 小时前
数据结构_深入理解堆(大根堆 小根堆)与优先队列:从理论到手撕实现
java·数据结构·算法
im_AMBER1 小时前
React 05
开发语言·前端·javascript·笔记·学习·react.js·前端框架
IT19951 小时前
OpenSSL3.5.2实现SM3数据摘要生成
算法·哈希算法·散列表