mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* Replace 结点 with 节点 Update the footnotes in the figures * Update mindmap * Reduce the size of the mindmap.png
		
			
				
	
	
		
			39 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
/**
 | 
						|
 * File: binary_tree_bfs.dart
 | 
						|
 * Created Time: 2023-04-03
 | 
						|
 * Author: liuyuxin (gvenusleo@gmai.com)
 | 
						|
 */
 | 
						|
 | 
						|
import 'dart:collection';
 | 
						|
import '../utils/print_util.dart';
 | 
						|
import '../utils/tree_node.dart';
 | 
						|
 | 
						|
/* 层序遍历 */
 | 
						|
List<int> levelOrder(TreeNode? root) {
 | 
						|
  // 初始化队列,加入根节点
 | 
						|
  Queue<TreeNode?> queue = Queue();
 | 
						|
  queue.add(root);
 | 
						|
  // 初始化一个列表,用于保存遍历序列
 | 
						|
  List<int> res = [];
 | 
						|
  while (queue.isNotEmpty) {
 | 
						|
    TreeNode? node = queue.removeFirst(); // 队列出队
 | 
						|
    res.add(node!.val); // 保存节点值
 | 
						|
    if (node.left != null) queue.add(node.left); // 左子节点入队
 | 
						|
    if (node.right != null) queue.add(node.right); // 右子节点入队
 | 
						|
  }
 | 
						|
  return res;
 | 
						|
}
 | 
						|
 | 
						|
/* Driver Code */
 | 
						|
void main() {
 | 
						|
  /* 初始化二叉树 */
 | 
						|
  // 这里借助了一个从数组直接生成二叉树的函数
 | 
						|
  TreeNode? root = listToTree([1, 2, 3, 4, 5, 6, 7]);
 | 
						|
  print("\n初始化二叉树\n");
 | 
						|
  printTree(root);
 | 
						|
 | 
						|
  // 层序遍历
 | 
						|
  List<int> res = levelOrder(root);
 | 
						|
  print("\n层序遍历的节点打印序列 = $res");
 | 
						|
}
 |