美文网首页
花样遍历二叉树

花样遍历二叉树

作者: 小丸子啦啦啦呀 | 来源:发表于2022-03-22 15:37 被阅读0次

数据结构定义

每个节点至多有两个孩子。

 function TreeNode(val, left, right) {
     this.val = (val===undefined ? 0 : val)
     this.left = (left===undefined ? null : left)
     this.right = (right===undefined ? null : right)
 }
binary tree

已上图二叉树为例,遍历顺序分别为:

  • BFS: [a,b,c,d,e,f]
  • DFS(preOrder): [a, b, d, c, e, f]
  • DFS(inOrder): [b, d, a, e, c, f]
  • DFS(postOrder): [d, b, e, f, c, a]

BFS(广度优先搜索,又名层次遍历)

BFS依靠队列来实现

递归遍历

function BFS(root){
   let queue = [root];
   recursive(queue);
   function recursive(queue){
     const node = queue.shift();
     if(!node)return;
     console.log(node.val);
     if(root.left)queue.push(node.left);
     if(root.right)queue.push(node.right);
     recursive()
   }
}

非递归遍历

function BFS(root){
   if(!root)return;
   let queue= [root];
   while(queue.length){
       const node = queue.shift();
       console.log(node.val);
       if(node.left)queue.push(node.left);
       if(node.right)queue.push(node.right);
   }
}

DFS(深度优先搜索)

所谓前序,中序,后序中的“序”是针对父节点而言。以先序举例,父节点在前面,则为前序,即NLR(根左右),那么NRL也是先序吗?根据维基百科的介绍,NRL是reverse preOrder, 因此也是先序。

preOrder(前序)

递归遍历

function preOrder(root){
   if(!root)return;
   console.log(root.val);
   if(root.left)preOrder(root.left);
   if(root.right)preOrder(root.right);
}

基于stack的非递归遍历

function preOrder(root){
  let stack = [root];
  let result = [];
  
  while(stack.length){
    const top = stack.pop();
    result.push(top.val);
    if(top.right)stack.push(top.right);
    if(top.left)stack.push(top.left);
  }
  return result;
}

inOrder(中序)

递归遍历

function inOrder(root){
   if(!root)return;
   if(root.left)preOrder(root.left);
   console.log(root.val);
   if(root.right)preOrder(root.right);
}

非递归遍历

function inOrder(node){
  let stack = [];
  let result = [];
  let current = node;
  
  while(current || stack .length){
    while(current){
      stack .push(current)
      current = current.left;
    }
    
    const top = stack .pop();
    result.push(top.val);
    current = top.right;
  }
  return result
}

postOrder(后序)

后序实际上就是中序的颠倒:
中左右 --- reverse ---> 右左中
所以可以把中序的结果倒转一下就得到了中序的遍历结果。

递归遍历

function postOrder(root){
   if(!root)return;
   if(root.left)preOrder(root.left);
   if(root.right)preOrder(root.right);
   console.log(root.val);
}

非递归遍历

function postOrder(root){
  let stack = [];
  let result = [];
  stack.push(root);
  while(stack.length){
    const top = stack.pop();
    result.push(top.val);
    if(top.left){
      stack.push(top.left);
    }
    if(top.right){
      stack.push(top.right);
    }
  }
  return result.reverse();
}

相关文章

  • 二叉树 基础操作

    二叉树的使用 二叉树结构 先序创建二叉树 DFS 先序遍历二叉树 中序遍历二叉树 后序遍历二叉树 BFS 层次遍历...

  • 关于二叉树的算法题

    前序遍历中序遍历后序遍历判断是否是平衡二叉树判断是否是对称二叉树判断二叉树高度按照层遍历二叉树判断二叉树宽度

  • 二叉树遍历

    二叉树 二叉树的存储结构 前序遍历 中序遍历 后序遍历 遍历代码 反转二叉树 深入学习二叉树 二叉树-你必须要懂!...

  • 二叉树操作

    树节点 逐行顺序解析二叉树 前序遍历二叉树 中序遍历二叉树 后序遍历二叉树 删除指定数值的节点 前序遍历顺序存储的...

  • 花样遍历二叉树

    数据结构定义 每个节点至多有两个孩子。 已上图二叉树为例,遍历顺序分别为: BFS: [a,b,c,d,e,f] ...

  • 数据结构与算法之二叉树遍历(七)

    目录 前序遍历中序遍历后序遍历层序遍历遍历方式的选择条件根据遍历结果重构二叉树翻转二叉树计算二叉树的高度判断一棵树...

  • 二叉树三种遍历Swift代码实现

    二叉树的三种遍历 二叉树 前序遍历 中序遍历 后序遍历 另外 不得不说,得到二叉树的前序遍历和中序遍历的结果或者后...

  • 二叉树的遍历

    二叉树的遍历 二叉树遍历 分为前序遍历、中序遍历和后序遍历。 前序遍历 (DLR) 先访问根节点,然后前序遍历左子...

  • 前端二叉树

    (一)构造二叉树 (二)中序遍历 (三)前序遍历 前序遍历可以复制二叉树,效率比重新构造二叉树高 (四)后序遍历 ...

  • 数据结构:树的实现和遍历(c++)

    (一)二叉树的遍历——递归实现 二叉树常见的遍历方式分为前序遍历、中序遍历和后序遍历。 1 前序遍历 前序遍历也叫...

网友评论

      本文标题:花样遍历二叉树

      本文链接:https://www.haomeiwen.com/subject/gvgolrtx.html