【劍指offer】樹的子結構
阿新 • • 發佈:2017-06-09
九度oj ac代碼 例子 order lan def mem targe 代碼
轉載請註明出處:http://blog.csdn.net/ns_code/article/details/25907685
劍指offer第18題,九度OJ上測試通過!
- 題目描寫敘述:
輸入兩顆二叉樹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為空樹時不是不論什麽樹的子樹
在寫這道題目時,卡在了測試代碼上,這個題目的測試代碼有點繁雜,最後參考了一哥的文章。改用數組作為存儲二叉樹節點的數據結構,果然寫測試代碼方便了非常多。
另外,程序中有一些要註意的地方,在程序中表明了凝視。
AC代碼:
#include<stdio.h> #include<stdlib.h> #include<stdbool.h> typedef struct BTNode { int data; int rchild; int lchild; }BTNode; /* 推斷pTree2是否是與pTree1有共同的根節點的pTree1子樹 */ bool isSubTree(BTNode *pTree1,int index1,BTNode *pTree2,int index2) { //前兩個if語句不能顛倒,不然當pTree1和pTree2同樣時,會誤判為false if(index2 == -1) return true; if(index1 == -1) return false; if(pTree1[index1].data != pTree2[index2].data) return false; else return isSubTree(pTree1,pTree1[index1].lchild,pTree2,pTree2[index2].lchild) && isSubTree(pTree1,pTree1[index1].rchild,pTree2,pTree2[index2].rchild); } /* 推斷pTree1是否包括pTree2 */ bool isContainTree(BTNode *pTree1,int index1,BTNode *pTree2,int index2) { if(pTree1==NULL || pTree2==NULL) return false; if(index1==-1 || index2==-1) return false; bool result = false; if(pTree1[index1].data == pTree2[index2].data) result = isSubTree(pTree1,index1,pTree2,index2); //假設pTree1[index1].lchild為-1,下次遞歸時會通過index1==-1的推斷返回false, //因此這裏不須要再加上pTree1[index1].lchild!=-1的推斷條件 if(!result) result = isContainTree(pTree1,pTree1[index1].lchild,pTree2,index2); if(!result) result = isContainTree(pTree1,pTree1[index1].rchild,pTree2,index2); return result; } int main() { int n,m; while(scanf("%d %d",&n,&m) != EOF) { //輸入樹pTree1各節點的值 BTNode *pTree1 = NULL; if(n>0) { pTree1 = (BTNode *)malloc(n*sizeof(BTNode)); if(pTree1 == NULL) exit(EXIT_FAILURE); int i,data; //輸入n個節點的data for(i=0;i<n;i++) { scanf("%d",&data); pTree1[i].data = data; pTree1[i].rchild = -1; pTree1[i].lchild = -1; } //輸入n行節點連接關系 for(i=0;i<n;i++) { int ki; scanf("%d",&ki); if(ki == 0) continue; else if(ki == 1) { int lindex; scanf("%d",&lindex); pTree1[i].lchild = lindex-1; } else { int lindex,rindex; scanf("%d",&lindex); scanf("%d",&rindex); pTree1[i].lchild = lindex-1; pTree1[i].rchild = rindex-1; } } } //輸入樹pTree2各節點的值 BTNode *pTree2 = NULL; if(m>0) { pTree2 = (BTNode *)malloc(m*sizeof(BTNode)); if(pTree2 == NULL) exit(EXIT_FAILURE); int i,data; //輸入n個節點的data for(i=0;i<m;i++) { scanf("%d",&data); pTree2[i].data = data; pTree2[i].rchild = -1; pTree2[i].lchild = -1; } //輸入n行節點連接關系 for(i=0;i<m;i++) { int ki; scanf("%d",&ki); if(ki == 0) continue; else if(ki == 1) { int lindex; scanf("%d",&lindex); pTree2[i].lchild = lindex-1; } else { int lindex,rindex; scanf("%d",&lindex); scanf("%d",&rindex); pTree2[i].lchild = lindex-1; pTree2[i].rchild = rindex-1; } } } if(isContainTree(pTree1,0,pTree2,0)) printf("YES\n"); else printf("NO\n"); } return 0; }
/**************************************************************
Problem: 1520
User: mmc_maodun
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/
【劍指offer】樹的子結構