bfs和dfs

来源:互联网 发布:网络妖人再造天txt 编辑:程序博客网 时间:2024/05/18 16:37

一、广度优先算法BFS(Breadth First Search)

基本实现思想

        (1)顶点v入队列。

        (2)当队列非空时则继续执行,否则算法结束。

        (3)出队列取得队头顶点v;

        (4)查找顶点v的所以子节点,并依次进入队列;

        (5)转到步骤(2)。

 

 python伪代码:

  def BFS(root)

    Q=[]

    Q.append(root[0])

    while len(Q)>0:

      node=Q.pop(0)

      print (node)

      #将所有子节点入队列

      for i in node_child:

        Q.append(node_child[i])

 

:该算法很巧妙的利用了队列的先进先出策略。

 

二、深度优先算法DFS(Depth First Search)

 

 

基本思想: 

递归实现:

             (1)访问顶点v,打印节点;

             (2)遍历v的子节点w,while(w存在),递归执行该节点;

代码:

  /布尔型数组Visited[]初始化成false  void DFS(Vetex v)  {      Visited[v] = true;      for each w adjacent to v          if (!Visited[w])              DFS(w);  }

 

非递归实现:

             (1)访问顶点v,顶点v入栈S,打印输出顶点,visited[v]=1

              (2)while(栈S非空)

                      x=栈S的顶元素(不出栈);

                      if(存在并找到未被访问的x的子结点w)

                        访问w,打印输出,visited[w]=1;w进栈;

                      else

                        x出栈;

 注:visited[x]=1,标记该节点已被访问



算法题:

107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3   / \  9  20    /  \   15   7

return its bottom-up level order traversal as:

[  [15,7],  [9,20],  [3]]
1.bfs (2ms)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list = new LinkedList<List<Integer>>();
        if(root==null) return list;
        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while(queue.size()>0){
            int num = queue.size();
            LinkedList<Integer> sublist = new LinkedList<Integer>();
            for(int i=0;i<num;i++){
                TreeNode node = queue.poll();
                if(node.left!=null) queue.add(node.left);
                if(node.right!=null) queue.add(node.right);
                
                sublist.add(node.val);
            }
            list.add(0,sublist);
            
        }
        return list;
    }
}


2.dfs (3ms)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list = new LinkedList<List<Integer>>();
        dfs(list,0,root);
        return list;
    }
    public void dfs(List<List<Integer>> list,int level,TreeNode node){
        if(node == null) return;
        if(level>=list.size()) list.add(0,new LinkedList<Integer>());
        list.get(list.size()-1-level).add(node.val);
        dfs(list,level+1,node.left);
        dfs(list,level+1,node.right);
    }
}

原创粉丝点击