mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
/**
 | 
						|
 * File: binary_tree_bfs.java
 | 
						|
 * Created Time: 2022-11-25
 | 
						|
 * Author: krahets (krahets@163.com)
 | 
						|
 */
 | 
						|
 | 
						|
package chapter_tree;
 | 
						|
 | 
						|
import utils.*;
 | 
						|
import java.util.*;
 | 
						|
 | 
						|
public class binary_tree_bfs {
 | 
						|
    /* Level-order traversal */
 | 
						|
    static List<Integer> levelOrder(TreeNode root) {
 | 
						|
        // Initialize queue, add root node
 | 
						|
        Queue<TreeNode> queue = new LinkedList<>();
 | 
						|
        queue.add(root);
 | 
						|
        // Initialize a list to store the traversal sequence
 | 
						|
        List<Integer> list = new ArrayList<>();
 | 
						|
        while (!queue.isEmpty()) {
 | 
						|
            TreeNode node = queue.poll(); // Queue dequeues
 | 
						|
            list.add(node.val);           // Save node value
 | 
						|
            if (node.left != null)
 | 
						|
                queue.offer(node.left);   // Left child node enqueues
 | 
						|
            if (node.right != null)
 | 
						|
                queue.offer(node.right);  // Right child node enqueues
 | 
						|
        }
 | 
						|
        return list;
 | 
						|
    }
 | 
						|
 | 
						|
    public static void main(String[] args) {
 | 
						|
        /* Initialize binary tree */
 | 
						|
        // Use a specific function to convert an array into a binary tree
 | 
						|
        TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
 | 
						|
        System.out.println("\nInitialize binary tree\n");
 | 
						|
        PrintUtil.printTree(root);
 | 
						|
 | 
						|
        /* Level-order traversal */
 | 
						|
        List<Integer> list = levelOrder(root);
 | 
						|
        System.out.println("\nPrint sequence of nodes from level-order traversal = " + list);
 | 
						|
    }
 | 
						|
}
 |