DS二叉樹—二叉樹結點的最大距離


題目描述

      二叉樹兩個結點的距離是一個結點經過雙親結點,祖先結點等中間結點到達另一個結點經過的分支數。二叉樹結點的最大距離是所有結點間距離的最大值。例如,下圖所示二叉樹結點最大距離是3,C和D的距離。

          二叉樹用先序遍歷順序創建,#表示空樹。計算二叉樹結點最大距離和最大距離的兩個結點(假設二叉樹中取最大距離的兩個結點唯一)。

 

 

輸入

測試次數T

第2行之后的T行,每行為一棵二叉樹先序遍歷結果(#表示空樹)

 

輸出

 對每棵二叉樹,輸出樹的結點最大距離和最大距離的結點,輸出格式見樣例。

樣例輸入

3 A## ABC##EF#G###D## ABEH###F#K###

樣例輸出

0: 5:G D 4:H K

提示

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int M[100];
char L[100];
char R[100];
int I=0;
class BiTreeNode
{
public:
    char data;
    BiTreeNode *Left;
    BiTreeNode *Right;
    BiTreeNode()
    {
        Left=NULL;
        Right=NULL;
    }
    ~BiTreeNode()
    {
        delete Left;
        delete Right;
    }
};
 
class BiTree
{
public:
    BiTreeNode *Root;
    int pos;
    string strTree;
    int deep;
    int leftdeep;
    int rightdeep;
    char leftc;
    char rightc;
    char c;
    int maxx;
    BiTree(string str)
    {
        pos=0;
        deep=0;
        leftdeep=0;
        rightdeep=0;
        maxx=0;
        strTree=str;
        Root=CreateBiTree();
    }
    BiTreeNode *CreateBiTree()
    {
        char ch=strTree[pos];
        pos++;
        if(ch=='#')
        {
            return NULL;
        }
        else
        {
            BiTreeNode *T;
            T=new BiTreeNode();
            T->data=ch;
            T->Left=CreateBiTree();
            T->Right=CreateBiTree();
            return T;
        }
    }
    void countleftdeep(BiTreeNode *p)
    {
        if(p->Left==NULL)
        {
            leftdeep=0;
            leftc=p->data;
            return;
        }
        p=p->Left;
        countdeep(p,0);
        leftdeep=deep;
        leftc=c;
        return;
    }
    void countrightdeep(BiTreeNode *p)
    {
        if(p->Right==NULL)
        {
            rightdeep=0;
            rightc=p->data;
            return;
        }
        else
            p=p->Right;
        countdeep(p,0);
        rightdeep=deep;
        rightc=c;
        return;
    }
    void countdeep(BiTreeNode *p,int i)
    {
        if(p)
        {
            i++;
            if(p->Left==NULL&&p->Right==NULL)
            {
                if(deep<i)
                {
                    deep=i;
                    c=p->data;
                }
            }
            countdeep(p->Left,i);
            countdeep(p->Right,i);
        }
    }
    void pre(BiTreeNode *p)//
    {
        if(p!=NULL)
        {
            deep=0;
            countleftdeep(p);//
            deep=0;
            countrightdeep(p);//
            deep=0;
            maxx=leftdeep+rightdeep;
            M[I]=maxx;
            L[I]=leftc;
            R[I]=rightc;
            I++;
            pre(p->Left);
            pre(p->Right);
        }
    }
};
 
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        for(int i=0;i<I;i++)
            M[i]=0;
        memset(L,0,sizeof(L));
        memset(R,0,sizeof(L));
        I=0;
        string str;
        cin>>str;
        BiTree tree(str);
        tree.pre(tree.Root);
        int m=M[0];
        int index=0;
        for(int i=0;i<I;i++)
        {
            if(M[i]>m)
                index=i;
        }
        cout<<M[index]<<":";
        if(M[index]!=0)
            cout<<L[index]<<" "<<R[index]<<endl;
        else
            cout<<endl;
    }
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM