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
// ]
相关推荐
codingandsleeping5 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
白水清风6 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
用户22152044278006 小时前
new、原型和原型链浅析
前端·javascript
阿星做前端6 小时前
coze源码解读: space develop 页面
前端·javascript
叫我小窝吧6 小时前
Promise 的使用
前端·javascript
前端康师傅7 小时前
JavaScript 作用域
前端·javascript
云枫晖7 小时前
JS核心知识-事件循环
前端·javascript
聚客AI8 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
eason_fan8 小时前
Git 大小写敏感性问题:一次组件重命名引发的CI构建失败
前端·javascript
前端付豪10 小时前
1、震惊!99% 前端都没搞懂的 JavaScript 类型细节
前端·javascript·面试