035樹的子結構(keep it up)

劍指offer中題目:http://ac.jobdu.com/problem.php?pid=1520

題目描述:

輸入兩顆二叉樹A,B,判斷B是不是A的子結構。

輸入:

輸入可能包含多個測試樣例,輸入以EOF結束。
對於每個測試案例,輸入的第一行一個整數n,m(1<=n<=1000,1<=m<=1000):n代表將要輸入的二叉樹A的節點個數(節點從1開始計數),m代表將要輸入的二叉樹B的節點個數(節點從1開始計數)。接下來一行有n個數,每個數代表A樹中第i個元素的數值,接下來有n行,第一個數Ki代表第i個節點的子孩子個數,接下來有Ki個樹,代表節點i子孩子節點標號。接下來m+1行,與樹A描述相同。

輸出:

對應每個測試案例,
若B是A的子樹輸出”YES”(不包含引號)。否則,輸出“NO”(不包含引號)。

樣例輸入:
7 3
8 8 7 9 2 4 7
2 2 3
2 4 5
0
0
2 6 7
0
0
8 9 2
2 2 3
0
0

1 1
2
0
3
0
樣例輸出:
YES
NO
提示:

B爲空樹時不是任何樹的子樹。

代碼:

#include <stdio.h>
#include <stdlib.h>
#include <queue>
 
#define MAXDATA 0X7FFFFFFF
#define MAXSIZE 1001
 
int TreeData[MAXSIZE];
 
typedef struct SNode
{
    int data;
    int left;
    int right;
}SNode;
 
SNode* createNode(int vData)
{
    SNode *Node = (SNode*)malloc(sizeof(SNode));
    Node->data  = vData;
    Node->left  = -1;
    Node->right = -1;
    return Node;
}
 
void deleteNode(SNode **vNode)
{
    (*vNode)->left  = -1;
    (*vNode)->right = -1;
    free(*vNode);
    *vNode = NULL;
}
 
void allocNode(SNode *vArray[], int vSize)
{
    int i;
    for (i = 1; i <= vSize; ++i)
    {
        vArray[i] = createNode(MAXDATA);
    }
}
 
void deleteTree(SNode *vTree[], int vSize)
{
    int i;
    for (i=1; i <= vSize; ++i)
    {
        deleteNode(&(vTree[i]));
    }
}
 
void clearTree(SNode *vTree[], int vSize)
{
    int i;
    for (i=1; i <= vSize; ++i)
    {
        vTree[i]->data  = MAXDATA;
        vTree[i]->left  = -1;
        vTree[i]->right = -1;
    }
}
 
void createBinaryTree(SNode *vNode[], int vN)
{
    int    i;
    int    NumChld;
 
    for (i = 1; i <= vN; ++i)
    {
        scanf("%d", &(vNode[i]->data));
    }
 
    for (i = 1; i <= vN; ++i)
    {
        scanf("%d", &NumChld);
        if (NumChld == 0) continue;
        else if (NumChld == 1)
        {
            scanf("%d", &(vNode[i]->left));
        }
        else if (NumChld == 2)
        {
            scanf("%d %d", &(vNode[i]->left), &(vNode[i]->right));
        }
    }
}
 
int judgeSubRoot(int vRootBegin, SNode *vTreeRoot[], int vSubBegin, SNode *vSubRoot[])
{
    int LeftFlag;
    int RightFlag;
     
    if (vSubBegin == -1)  return 1;
    if (vRootBegin == -1) return 0;
 
    if (vTreeRoot[vRootBegin]->data == vSubRoot[vSubBegin]->data) 
    {
        LeftFlag = judgeSubRoot(vTreeRoot[vRootBegin]->left, vTreeRoot, vSubRoot[vSubBegin]->left, vSubRoot);
        if (!LeftFlag) return 0;
        RightFlag = judgeSubRoot(vTreeRoot[vRootBegin]->right, vTreeRoot, vSubRoot[vSubBegin]->right, vSubRoot);
        if (!RightFlag) return 0;
        return 1;
    }
    else return 0;
}
 
int isSubTree(SNode *vTreeRoot[], SNode *vSubRoot[])
{
    if (vSubRoot[1]->data == MAXDATA || vTreeRoot[1]->data == MAXDATA) return 0;
 
    std::queue<int> Que;
    int Index;
    int Flag;
 
    Que.push(1);
    while(!Que.empty())
    {
        Index = Que.front();
        Que.pop();
 
        if (vTreeRoot[Index]->data == vSubRoot[1]->data)
        {
            Flag = judgeSubRoot(Index, vTreeRoot, 1, vSubRoot);
            if (Flag) return 1;
        }
 
        if (vTreeRoot[Index]->left != -1)  Que.push(vTreeRoot[Index]->left);
        if (vTreeRoot[Index]->right != -1) Que.push(vTreeRoot[Index]->right);
    }
 
    return 0;
}
 
int main()
{
    int M;
    int N;
    SNode *TreeRoot[MAXSIZE];
    SNode *SubRoot[MAXSIZE];
 
    allocNode(TreeRoot, MAXSIZE-1);
    allocNode(SubRoot, MAXSIZE-1);
 
    while (scanf("%d %d", &N, &M) != EOF)
    {
        createBinaryTree(TreeRoot, N);
        createBinaryTree(SubRoot, M);
 
        if (isSubTree(TreeRoot, SubRoot))
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
 
        clearTree(TreeRoot, MAXSIZE-1);
        clearTree(SubRoot, MAXSIZE-1);
    }
 
    deleteTree(TreeRoot, MAXSIZE-1);
    deleteTree(SubRoot, MAXSIZE-1);
 
    return 0;
}
/**************************************************************
    Problem: 1520
    User: 
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1056 kb
****************************************************************/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章