How do implement a breadth first traversal?

前端 未结 9 1365
萌比男神i
萌比男神i 2020-11-28 21:34

This is what I have. I thought pre-order was the same and mixed it up with depth first!

import java.util.LinkedList;
import java.util.Queue;

public class Ex         


        
9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 22:17

    This code which you have written, is not producing correct BFS traversal: (This is the code you claimed is BFS, but in fact this is DFS!)

    //  search traversal
      public void breadth(TreeNode root){
          if (root == null)
              return;
    
          System.out.print(root.element + " ");
          breadth(root.left);
          breadth(root.right);
     }
    

提交回复
热议问题