美文网首页
算法-Tree广度优先搜索

算法-Tree广度优先搜索

作者: 码农朱同学 | 来源:发表于2022-08-10 17:51 被阅读0次

Tree

类似LinkedList的概念,内存中不一定连续的数据,由各个节点的Reference串起来组成。

  • 节点可以分为ParentChild两类
  • 可以看做一个特殊的无环无向图
  • 只有一个入口(root)

BFS(Breadth-First Search)

BFS 是按“层”的概念进行的搜索算法,利用Queue记录需要被展开的 TreeNode。
适合解决与层数相关的Tree题目

实例

/*
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

例如:
给定二叉树[3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回锯齿形层次遍历如下:

[
  [3],
  [20,9],
  [15,7]
]

 */
class LeetCode103 {
    public static void main(String[] args) {

        TreeNode node0 = TreeNode.buildBinaryTree(new Integer[]{1, 2, 3, null, 4, null, 5, null, 6});

        List<List<Integer>> arr = new Solution().zigzagLevelOrder(node0);

        System.out.println(arr);
    }

    /*
    算法

实现 BFS 的几种算法。

使用两层嵌套循环。外层循环迭代树的层级,内层循环迭代每层上的节点。

也可以使用一层循环实现 BFS。将要访问的节点添加到队列中,使用 分隔符(例如:空节点)把不同层的节点分隔开。分隔符表示一层结束和新一层开始。

这里采用第二种方法。在此算法的基础上,借助双端队列实现锯齿形顺序。在每一层,使用一个空的双端队列保存该层所有的节点。根据每一层的访问顺序,即从左到右或从右到左,决定从双端队列的哪一端插入节点。

     */

    static class Solution {
        public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            if (root == null) {
                return new ArrayList<>();
            }
            List<List<Integer>> results = new ArrayList<>();

            // add the root element with a delimiter to kick off the BFS loop
            LinkedList<TreeNode> node_queue = new LinkedList<>();
            node_queue.addLast(root);
            node_queue.addLast(null);

            LinkedList<Integer> level_list = new LinkedList<>();
            boolean is_order_left = true;

            while (node_queue.size() > 0) {
                TreeNode curr_node = node_queue.pollFirst();
                if (curr_node != null) {
                    if (is_order_left)
                        level_list.addLast(curr_node.val);
                    else
                        level_list.addFirst(curr_node.val);

                    if (curr_node.left != null)
                        node_queue.addLast(curr_node.left);
                    if (curr_node.right != null)
                        node_queue.addLast(curr_node.right);

                } else {
                    // we finish the scan of one level
                    results.add(level_list);
                    level_list = new LinkedList<>();
                    // prepare for the next level
                    if (node_queue.size() > 0)
                        node_queue.addLast(null);
                    is_order_left = !is_order_left;
                }
            }
            return results;
        }
    }
}

相关文章

  • 算法-Tree广度优先搜索

    Tree 类似LinkedList的概念,内存中不一定连续的数据,由各个节点的Reference串起来组成。 节点...

  • 算法与数据结构 之 搜索算法

    搜索分为广度优先搜索、深度优先搜索、A*算法。 一、广度优先算法(BFS) 1.1、基本实现和特性:BFS是从一个...

  • 广度优先搜索算法

    上一篇简书小编分享了“深度优先搜索”算法,今天小编继续分享下“广度优先搜索”算法。 一、何为“广度优先搜索” 广度...

  • 广度优先搜索算法(BFS)

    广度优先搜索算法(BFS) 标签(空格分隔): algorithm 1.广度优先搜索算法(Breadth Firs...

  • 算法(三):图解广度优先搜索算法

    算法简介 广度优先搜索算法(Breadth First Search),又称为"宽度优先搜索"或"横向优先搜索",...

  • 搜索

    一、深度优先搜索 图深度优先遍历、深度优先搜索算法求有权图两点最短路径 二、广度优先搜索 图广度优先遍历、广度优先...

  • python 广度优先算法

    文章概述 广度优先搜索算法概述 广度优先搜索算法 广度优先算法是基于树或者图的,当然树也是一种特殊的图,因此我们先...

  • 【数据结构】广度优先搜索算法BFS

    对于广度优先遍历算法DFS可以参考前一篇文章【数据结构】深度优先搜索算法DFS 广度优先遍历 广度优先遍历(Bre...

  • 算法之广度优先搜索(BFS)

    图算法——广度优先搜索 (breadth-first search,BFS)。广度优先搜索让你能够找出两样东西之间...

  • 算法图解 (六)

    第六章 广度优先搜索 广度优先搜索算法 (英文: Breadth-First-Search, 缩写为BFS), 又...

网友评论

      本文标题:算法-Tree广度优先搜索

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