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
// ]
相关推荐
zyl7651 小时前
FlashAttention 详解:从 GPU 硬件、普通 Attention 到分块计算与 Online Softmax
算法
OpsEye3 小时前
大模型账单对账难:不同厂商 Token 统计口径差异怎么处理
javascript·ai编程
全栈项目管理程序猿3 小时前
ArcGIS JS 基础教程(32):体元系列 - VoxelSlice 体元切片
前端·javascript
爱读书的小胖5 小时前
LeetCode 122:买卖股票的最佳时机 II(C)解题分析
算法
结局无敌5 小时前
React Native for OpenHarmony 实战:三方库 react-native-modal-dialog 的鸿蒙化适配指南
深度学习·算法·react native·react.js·harmonyos
OPEN-F5 小时前
ROS2系列教程:上位机建图与导航(原理综合)
算法·机器人
weixin_307779135 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
Hilaku5 小时前
AI 流式输出都用 SSE,什么时候必须换 WebSocket?
前端·javascript·程序员
奇牙coding5 小时前
Codex CLI 升级后多文件编辑静默失效:config.yaml 枚举变更排查与修复指南
java·前端·javascript·ai
tomwang03225 小时前
路径优化建模、优化算法分类及工程化小结
算法·数学建模·分类