mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 11:29:51 +08:00
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
42 lines
1.3 KiB
C++
42 lines
1.3 KiB
C++
/**
|
|
* File: binary_tree_bfs.cpp
|
|
* Created Time: 2022-11-25
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
/* レベル順走査 */
|
|
vector<int> levelOrder(TreeNode *root) {
|
|
// キューを初期化、ルートノードを追加
|
|
queue<TreeNode *> queue;
|
|
queue.push(root);
|
|
// 走査順序を保存するリストを初期化
|
|
vector<int> vec;
|
|
while (!queue.empty()) {
|
|
TreeNode *node = queue.front();
|
|
queue.pop(); // キューからデキュー
|
|
vec.push_back(node->val); // ノード値を保存
|
|
if (node->left != nullptr)
|
|
queue.push(node->left); // 左の子ノードをエンキュー
|
|
if (node->right != nullptr)
|
|
queue.push(node->right); // 右の子ノードをエンキュー
|
|
}
|
|
return vec;
|
|
}
|
|
|
|
/* ドライバーコード */
|
|
int main() {
|
|
/* 二分木を初期化 */
|
|
// 特定の関数を使用して配列を二分木に変換
|
|
TreeNode *root = vectorToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
|
cout << endl << "二分木を初期化\n" << endl;
|
|
printTree(root);
|
|
|
|
/* レベル順走査 */
|
|
vector<int> vec = levelOrder(root);
|
|
cout << endl << "レベル順走査のノード順序 = ";
|
|
printVector(vec);
|
|
|
|
return 0;
|
|
} |