Files
hello-algo/ru/codes/swift/chapter_tree/binary_tree_bfs.swift
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

43 lines
1.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* File: binary_tree_bfs.swift
* Created Time: 2023-01-18
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* Обход в ширину */
func levelOrder(root: TreeNode) -> [Int] {
// Инициализировать очередь и добавить корневой узел
var queue: [TreeNode] = [root]
// Инициализировать список для хранения последовательности обхода
var list: [Int] = []
while !queue.isEmpty {
let node = queue.removeFirst() // Извлечение из очереди
list.append(node.val) // Сохранить значение узла
if let left = node.left {
queue.append(left) // Поместить левый дочерний узел в очередь
}
if let right = node.right {
queue.append(right) // Поместить правый дочерний узел в очередь
}
}
return list
}
@main
enum BinaryTreeBFS {
/* Driver Code */
static func main() {
/* Инициализация двоичного дерева */
// Здесь используется функция, напрямую строящая двоичное дерево из массива
let node = TreeNode.listToTree(arr: [1, 2, 3, 4, 5, 6, 7])!
print("\nИнициализация двоичного дерева\n")
PrintUtil.printTree(root: node)
/* Обход в ширину */
let list = levelOrder(root: node)
print("\nПоследовательность печати узлов при обходе в ширину = \(list)")
}
}