zoukankan      html  css  js  c++  java
  • 完全二叉树指向同一层的相邻结点

    题目:对于一颗完全二叉树,要求给所有节点加上一个pNext指针,指向同一层的相邻节点;如果当前节点已经是该层的最后一个节点,则将pNext指针指向NULL;给出程序实现,并分析时间复杂度和空间复杂度。

    答:时间复杂度为O(n),空间复杂度为O(1)。

    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    struct TreeNode 
    {
        int m_nValue;
        TreeNode *m_pLeft;
        TreeNode *m_pRight;
        TreeNode *pNext;
    };
    
    //假定所创建的二叉树如下图所示
    /*
                                                 1
                                              /     \
                                             2       3
                                            / \      / \
                                           4   5    6   7
                                          / \  / \  / \
                                         8   9 10 11 12 13
    */
    void CreateBitree(TreeNode *&pNode, fstream &fin)
    {
        int dat;
        fin>>dat;
        if(dat == 0)
        {
            pNode = NULL;
        }
        else 
        {
            pNode = new TreeNode();
            pNode->m_nValue = dat;
            pNode->m_pLeft = NULL;
            pNode->m_pRight = NULL;
            pNode->pNext = NULL;
            CreateBitree(pNode->m_pLeft, fin);      
            CreateBitree(pNode->m_pRight, fin); 
        }
    }
    
    //完全二叉树指向同一层的相邻结点
    void Solution(TreeNode *pHead)
    {
        if (NULL == pHead)
        {
            return;
        }
        vector<TreeNode*> vec;
        vec.push_back(pHead);
        TreeNode *pre = NULL;
        TreeNode *pNode = NULL;
        int cur = 0;
        int last = 0;
        while(cur < vec.size())
        {
            last = vec.size();
            while (cur < last)
            {
                if (NULL == pre)
                {
                    pre = vec[cur];
                }
                else
                {
                    pre->pNext = vec[cur];
                }
                if (NULL != vec[cur]->m_pLeft)
                {
                    vec.push_back(vec[cur]->m_pLeft);
                }
                if (NULL != vec[cur]->m_pRight)
                {
                    vec.push_back(vec[cur]->m_pRight);
                }
                pre = vec[cur];
                cur++;
            }
            pre->pNext = NULL;
            pre = NULL;
        }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        fstream fin("tree.txt");
        TreeNode *pHead = NULL;
        TreeNode *pNode = NULL;
        CreateBitree(pHead, fin);
        Solution(pHead);
    
        while (NULL != pHead)
        {
            cout<<pHead->m_nValue<<"  ";
            pNode = pHead->pNext;
            while (NULL != pNode)
            {
                cout<<pNode->m_nValue<<"  ";
                pNode = pNode->pNext;
            }
            cout<<endl;
            pHead = pHead->m_pLeft;
        }
    
        cout<<endl;
        return 0;
    }

    运行界面如下:

    建造二叉树用到的tree.txt文件如下:

    1 2 4 8 0 0 9 0 0 5 10 0 0 11 0 0 3 6 12 0 0 13 0 0 7 0 0
  • 相关阅读:
    ffplay 2.5.3 媒体播放器
    MinGW/MSYS 交叉编译环境搭建
    python chm 中文帮助 (2.7 和 3.4)
    wx.html2.WebView在 target="_blank" or rel="external" 没有反映的解决方法
    韩星5,6号 一锅双星技巧
    暖房子工程
    CStringUtf8ToUnicode
    燃气灶中心炉芯帽子生锈了,如何拆不下来?
    翻窗户消失的百岁老人/百岁老人跷家去 中文字幕
    CPinyin unicode汉字查找拼音(支持多音字)
  • 原文地址:https://www.cnblogs.com/venow/p/2670520.html
Copyright © 2011-2022 走看看