LeetCode二叉树:递归遍历&&迭代遍历&&层序遍历

LeetCode二叉树:递归遍历&&迭代遍历&&层序遍历

递归遍历

  • 前序遍历
/**
 * 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 dfs = function(root) {
    if(root === null) {
      return;
    } 
    //先序遍历从父节点开始
    res.push(root.val)
    //递归左子树
    dfs(root.left)
    //递归右子树
    dfs(root.right)
  }
  dfs(root)
  return res
};

  • 中序遍历
var inorderTraversal = function(root) {
  let res = []
  const dfs = function(root) {
    if(root === null) {
      return;
    }
    dfs(root.left)
    res.push(root.val)
    dfs(root.right)
  }
  dfs(root)
  return res
};

  • 后序遍历
var postorderTraversal = function(root) {
  let res = []
  const dfs = function(root) {
    if(root === null) {
      return;
    }
    dfs(root.left)
    dfs(root.right)
    res.push(root.val)
  }  
  dfs(root)
  return res
};

迭代遍历Ⅰ

前序遍历是中左右,每次先处理的是中间节点,那么先将根节点放入栈中,然后将右孩子加入栈,再加入左孩子。

为什么要先加入 右孩子,再加入左孩子呢? 因为这样出栈的时候才是中左右的顺序。

  • 前序遍历
var preorderTraversal = function(root) {
  let res = []
  //注:这里不能写成{return;},写分号是dfs用来结束条件的,
  if(root === null){
    return res
  }
  let cur = null
  const stack = [root]
  while(stack.length) {
    cur = stack.pop()
    res.push(cur.val)
    cur.right && stack.push(cur.right)
    cur.left && stack.push(cur.left)
  }
  return res
};
  • 后序遍历
    先序遍历是 中左右 --> 调整代码左右顺序:中右左 --> 翻转result数组:左右中
    后序遍历:左右中
var postorderTraversal = function(root) {
  let res = []
  if(root === null) {
    return res
  }
  const stack = [root]
  let cur = null
  while(stack.length) {
    cur = stack.pop()
    res.push(cur.val)
    cur.left && stack.push(cur.left)
    cur.right && stack.push(cur.right) 
  }
  return res.reverse()
};    
  • 中序遍历
    中序遍历是左中右,先访问的是二叉树顶部的节点,然后一层一层向下访问,直到到达树左面的最底部,再开始处理节点(也就是在把节点的数值放进result数组中)
    处理顺序和访问顺序是不一致的。
    在使用迭代法写中序遍历,就需要借用指针的遍历来帮助访问节点,栈则用来处理节点上的元素。
var inorderTraversal = function(root) {
  let res = []
  if(root === null) {
    return res
  }
  const stack = []
  let cur = root
  while(stack.length || cur) {
    if(cur) {
      stack.push(cur)
      cur = cur.left //左
    } else {
      cur = stack.pop() //中
      res.push(cur.val)
      cur = cur.right //右
    }
  }
  return res
};


迭代遍历Ⅱ

这里的迭代遍历是统一迭代,因为前面的代码无法通过改一两行代码实现中序遍历

  • 前序遍历
/*
 前序遍历:中左右
 压栈顺序:右左中
*/
var preorderTraversal = function(root) {
  let res = []
  const stack =[]
  if(root) stack.push(root)
  while(stack.length) {
    const node = stack.pop()
    if(!node) {
      res.push(stack.pop().val)
      continue
    }
    if(node.right) stack.push(node.right) //右
    if(node.left) stack.push(node.left) //左
    stack.push(node) //中
    stack.push(null)
  }
  return res
};
  • 中序遍历
//# 中序遍历
/*
  中序遍历:左中右
  压栈顺序:右中左
*/
var inorderTraversal = function(root) {
  let res = []
  const stack = []
  if(root) stack.push(root)
  while(stack.length) {
    const node = stack.pop()
    if(!node) {
      res.push(stack.pop().val)
      continue
    }
    if(node.right) stack.push(node.right) //右
    stack.push(node) //中
    stack.push(null)
    if(node.left) stack.push(node.left) //左
  }
  return res
};
  • 后序遍历
/*
 后续遍历:左右中
 压栈顺序:中右左
*/
var postorderTraversal = function(root) {
  let res = []
  const stack = []
  if(root) stack.push(root)
  while(stack.length) {
    const node = stack.pop()
    if(!node) {
      res.push(stack.pop().val)
      continue
    }
    stack.push(node) //中
    stack.push(null)
    if(node.right) stack.push(node.right) //有
    if(node.left) stack.push(node.left) //左
  }
  return res
}; 

层序遍历

var levelOrder = function(root) {
  let res = [],queue = []
  queue.push(root)
  if(root === null) {
    return res
  }
  while(queue.length) {
    //记录当前层级节点数
    let length = queue.length
    //存放每一层的节点
    let curlevel = []
    for(let i=0;i<length;i++) {
      let node = queue.shift()
      curlevel.push(node.val)
      node.left && queue.push(node.left)
      node.right && queue.push(node.right)
    }
    //把每一层的结果放到结果数组
    res.push(curlevel)
  }
  return res
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛小y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值