mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Rewrite the tree serialization and deserialization methods. Add applications of array and linked list.
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.2 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;
 | 
						|
}
 | 
						|
 | 
						|
/* Driver Code */
 | 
						|
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;
 | 
						|
}
 |