欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++實現(xiàn)版本層次遍歷功能

 更新時間:2021年08月06日 11:15:45   作者:花與不易🐟  
這篇文章主要介紹了c++實現(xiàn)版本層次遍歷功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

采用隊列實現(xiàn),BFS,功能:BFS層次遍歷打印、按照節(jié)點將BFS序列化成一個字符。

#include <iostream>
#include <string>
#include <queue>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int val) : val(val), left(nullptr), right(nullptr) {}
};

//迭代打印層次遍歷
void BFSTraverse(TreeNode* root)
{
    queue<TreeNode*> nodeQueue;
    nodeQueue.push(root);//先把第一個先放到列表里面
    while (!nodeQueue.empty())
    {
        int sz = nodeQueue.size();//這個是為了一層一層的數(shù)值進行處理
        for (int i = 0; i < sz; i++)
        {
            //那就取出那個節(jié)點進行處理
            TreeNode* node = nodeQueue.front();
            cout << node->val << ", ";
            nodeQueue.pop();
            if (node->left)
            {
                nodeQueue.push(node->left);
            }
            if (node->right)
            {
                nodeQueue.push(node->right);
            }
        }
    }
}


//按照節(jié)點進行序列化成一個字符串
string serialByBFS(TreeNode* root)
{
    if (root == nullptr)
        return "#!";
    queue<TreeNode*> nodeQueue;
    nodeQueue.push(root);
    string res;
    while (!nodeQueue.empty())
    {
        int sz = nodeQueue.size();
        for (int i = 0; i < sz; i++)
        {
            TreeNode* node = nodeQueue.front();
            nodeQueue.pop();
            if (node)
            {
                res = res + std::to_string(node->val) + '!';
                nodeQueue.push(node->left);
                nodeQueue.push(node->right);
            }
            else
            {
                res = res + "#!";
            }
        }
    }
    return res;
}


int main3()
{
    TreeNode* head = new TreeNode(5);
    head->left = new TreeNode(3);
    head->right = new TreeNode(8);
    head->left->left = new TreeNode(1);
    head->left->right = new TreeNode(2);
    head->right->left = new TreeNode(4);
    head->right->right = new TreeNode(5);
    head->right->left->left = new TreeNode(6);
    head->right->right->left = new TreeNode(9);
    head->right->right->right = new TreeNode(11);

    cout << "traverse1:";
    BFSTraverse(head);
    cout << "\nserial binary:";
    string res = serialByBFS(head);
    cout << res << endl;

    return 0;
}

ps:下面看下C++層次遍歷

/*
*   description:層次遍歷
*   writeby:    nick
*   date:       2012-10-22 23:56
*/
#include <iostream>
#include <queue>

using namespace std;

struct node
{
    int item;
    node *l, *r;
    node(int n)
    {
        item=n;
        l=0;
        r=0;
    }
};
typedef node *link;


void traverse(link h, void visit(link))
{
    queue<link> q;
    q.push(h);
    while(!q.empty())
    {
        h = q.front();
        q.pop();
        visit(h);
        if(h->l != 0) q.push(h->l);
        if(h->r !=0) q.push(h->r);
    }
}

void visit(link p)
{
    cout << p->item <<  " ";
}

int main()
{
    link root = new node(4);
    root->l = new node(5);
    root->r = new node(6);
    root->l->l = new node(7);
    root->l->r = new node(8);

    cout << "中文";
    traverse(root, visit);

    return 0;
}

到此這篇關于c++實現(xiàn)版本層次遍歷功能的文章就介紹到這了,更多相關c++層次遍歷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C語言實現(xiàn)簡易文本編輯器

    C語言實現(xiàn)簡易文本編輯器

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡易文本編輯器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • MFC模擬實現(xiàn)自定義消息發(fā)送

    MFC模擬實現(xiàn)自定義消息發(fā)送

    在MFC框架下,有很多系統(tǒng)已經(jīng)定義好的消息,例如ON_WM_LBUTTONDOWN()、ON_WM_MBUTTONDOWN()等等。本文將利用這些定義好的消息模擬實現(xiàn)一下消息發(fā)送,需要的可以參考一下
    2022-01-01
  • 淺析C++構造函數(shù)虛擬化

    淺析C++構造函數(shù)虛擬化

    這篇文章主要介紹了C++構造函數(shù)虛擬化的相關資料,文中講解非常細致,幫助大家更好的理解和學習c++構造函數(shù),感興趣的朋友可以了解下
    2020-08-08
  • C++中的異或運算符^的使用方法

    C++中的異或運算符^的使用方法

    本篇文章對C++中的異或運算符^的使用方法進行的詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言中順序棧和鏈棧的定義和使用詳解

    C語言中順序棧和鏈棧的定義和使用詳解

    這篇文章主要為大家詳細介紹了C語言中順序棧和鏈棧的定義和使用,文中的示例代碼講解詳細,對我們學習C語言有一定的幫助,感興趣的小伙伴可以了解一下
    2022-10-10
  • C++實現(xiàn)基于控制臺界面的吃豆子游戲

    C++實現(xiàn)基于控制臺界面的吃豆子游戲

    這篇文章主要介紹了C++實現(xiàn)基于控制臺界面的吃豆子游戲,實例分析了吃豆子游戲的原理與C++實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • Visual Studio 2019 Professional 激活方法詳解

    Visual Studio 2019 Professional 激活方法詳解

    這篇文章主要介紹了Visual Studio 2019 Professional 激活方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • C語言中無符號數(shù)和有符號數(shù)之間的運算

    C語言中無符號數(shù)和有符號數(shù)之間的運算

    C語言中有符號數(shù)和無符號數(shù)進行運算默認會將有符號數(shù)看成無符號數(shù)進行運算,其中算術運算默認返回無符號數(shù),邏輯運算當然是返回0或1了。下面通過一個例子給大家分享C語言中無符號數(shù)和有符號數(shù)之間的運算,一起看看吧
    2017-09-09
  • C++實現(xiàn)拓撲排序(AOV網(wǎng)絡)

    C++實現(xiàn)拓撲排序(AOV網(wǎng)絡)

    這篇文章主要為大家詳細介紹了C++實現(xiàn)拓撲排序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C++基于控制臺實現(xiàn)的貪吃蛇小游戲

    C++基于控制臺實現(xiàn)的貪吃蛇小游戲

    這篇文章主要介紹了C++基于控制臺實現(xiàn)的貪吃蛇小游戲,實例分析了貪吃蛇游戲的原理與C++實現(xiàn)技巧,是非常經(jīng)典的游戲算法,需要的朋友可以參考下
    2015-04-04

最新評論