二叉树前序/中序/后序/层序遍历

leetcode144:前序遍历:根结点 -> 左子树 -> 右子树

递归实现:

var preorderTraversal = function (root) { // 所有遍历函数的入参都是树的根结点对象
  const res = []

  function preOrder(root) {
    if (!root) return // 递归边界,root 为空
    res.push(root.val) //当前遍历的结点值
    preOrder(root.left) // 递归遍历左子树 
    preOrder(root.right) // 递归遍历右子树  
  }
  preOrder(root)
  return res
};

迭代实现: 

var preorderTraversal = function (root) { // 所有遍历函数的入参都是树的根结点对象
  const res = [] //定义结果数组
  if (!root) return [] //处理边界条件
  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
};

leetcode94:中序遍历:左子树 -> 根结点 -> 右子树

递归实现:

var inorderTraversal = function (root) {
  const res = []

  function inorder(root) {
    if (!root) return // 递归边界,root 为空
    inorder(root.left) // 递归遍历左子树 
    res.push(root.val) //当前遍历的结点值   
    inorder(root.right) // 递归遍历右子树  
  }
  inorder(root)
  return res
};

迭代实现:

var inorderTraversal = function (root) {
  const res = [] //定义结果数组
  const stack = [] //初始化栈结构
  let cur = root // 用一个cur当游标
  while (cur || stack.length) { // 当 cur 不为空、或者 stack 不为空时,重复以下逻辑
    while (cur) { // 这个 while 的作用是把寻找最左叶子结点的过程中,途径的所有结点都记录下来 
      stack.push(cur)
      cur = cur.left
    }

    //取出栈顶元素
    cur = stack.pop()
    //将栈顶元素
    res.push(cur.val)
    cur = cur.right // 尝试读取 cur 结点的右孩子
  }
  return res
};

leetcode145:后序遍历:左子树 ->右子树 ->根结点

递归实现:

var postorderTraversal = function(root) {
  const res = []
  const postOrder = (root) => {
    if (!root) return
    postOrder(root.left) // 递归遍历左子树 
    postOrder(root.right) // 递归遍历右子树 
    res.push(root.val) //当前遍历的结点值 
  }
  postOrder(root)
  return res
};

迭代实现:

var inorderTraversal = function (root) {
  const res = [] //定义结果数组
  if (!root) return [] //处理边界条件
  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
};

leetcode102:层序遍历:广度优先搜索

var levelOrder = function (root) {
  const res = [] //初始化结果数组
  if(!root) return [] //处理边界条件
  const queue = [] //初始化队列
  queue.push(root) //根节点先入列
  while (queue.length) { // 队列不为空,说明没有遍历完全
    let subRes = []
    for (let i = 0; i < queue.length; i++) {
      let cur = queue.shift() //取出队头元素
      subRes.push(cur.val)
      if (cur.left) queue.push(cur.left)
      if (cur.right) queue.push(cur.right)      
    }
    res.push(subRes)
  }
  return res

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值