階層印刷ツリーアルゴリズムC++で実現


#include
#include
#include
#include
using namespace std;
vector<char>::size_type n=0;
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x):val(x), left(NULL), right(NULL) 
    {
    }
};
class Serialize
{
public:
    void serialize(TreeNode* head,vector<char> &B)
    //     :B       
    {
        if(!head)
        {
            B.push_back('#');
            B.push_back('!');
            return;
        }
        int value=head->val;
        if(!value)
        {
            B.push_back('0');
            B.push_back('!');
            serialize(head->left,B);
            serialize(head->right,B);
            return;
        }
        int a=0;
        vector<int> A;
        while(value)
        {
            a=value%10;
            A.push_back(a);
            value/=10;
        }
        while(!A.empty())
        {
            a=A.back();
            A.pop_back();
            B.push_back('0'+a);
        }
        B.push_back('!');
        serialize(head->left,B);
        serialize(head->right,B);
        return;
    }

};
class DeSerialize
{
public:
    void deserialize(vector<char> A,TreeNode* & head)
    //      :#   ,!      ,n          ,A      
    {
        int i,j,value=0;
        if((n>A.size()-1)||A[n]=='#')
            return;
        i=j=n;
        while(A[j]!='!')
                j++;
        while(j>i)
        {
            value=(A[i]-'0')+value*10;
            i++;
        }
        head=(TreeNode*)malloc(sizeof(TreeNode));
        (*head).val=value;
        (*head).right=(*head).left=NULL;
        n=i+1;
        deserialize(A,(*head).left);
        deserialize(A,(*head).right);
    }

};

class TreePrinter {
public:
    vector<vector<int> > printTree(TreeNode* root) 
    {
        vector<vector<int> > res;
        vector<int> temp;
        queue que;
        que.push(root);
        TreeNode *last=root,*nlast=NULL,*Now=NULL;
        while(!que.empty())
        {
            Now=que.front();
            cout<val;
            temp.push_back(Now->val);
            if(Now->left)
            {
                que.push(Now->left);
                nlast=Now->left;
            }
            if(Now->right)
            {
                que.push(Now->right);
                nlast=Now->right;
            }
            if(Now==last)
            {
                res.push_back(temp);
                temp.clear();
                cout<return res;
    }
};
int main()
{
    char a[11]={'1','2','!','3','!','#','!','#','!','#','!'};
    vector<char> sor(a,a+11);
    DeSerialize b;
    TreeNode* T=NULL;
    b.deserialize(sor,T);
    Serialize c;
    vector<char> res;
    c.serialize(T,res);
    for(int i=0;i!=res.size();i++)
        cout<" ";
    cout<vector<vector<int> > res1;
    TreePrinter d;
    res1=d.printTree(T);
    for(vector<vector<int> >::iterator iter1=res1.begin();iter1!=res1.end();iter1++)
    {
        for(vector<int>::iterator iter2=(*iter1).begin();iter2!=(*iter1).end();iter2++)
            cout<" ";
        cout<return 0;
}