给定两个二叉树T和S,判断S是否为T的子树
生活随笔
收集整理的這篇文章主要介紹了
给定两个二叉树T和S,判断S是否为T的子树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
#include<iostream> #include <stdlib.h> using namespace std; struct node{int data;node* leftchild;node* rightchild; };bool isSubtree(node* root1,node* root2){if(root2==NULL)return true;if(root1==NULL)return false;if(root1->data==root2->data && isSubtree(root1->leftchild,root2->leftchild) &&isSubtree(root1->rightchild,root2->rightchild))return true;if(isSubtree(root1->leftchild,root2)||isSubtree(root1->rightchild,root2))return true;elsereturn false; }struct node* newNode(int i){struct node* node =(struct node*)malloc(sizeof(struct node));node->data=i;node->leftchild=NULL;node->rightchild=NULL;return node; }int main(){struct node *T = newNode(26);T->rightchild = newNode(3);T->rightchild->rightchild = newNode(3);T->leftchild = newNode(10);T->leftchild->leftchild = newNode(4);T->leftchild->leftchild->rightchild= newNode(30);T->leftchild->rightchild = newNode(6);struct node *S = newNode(10);S->rightchild = newNode(6);S->leftchild = newNode(4);if( isSubtree(T, S) )printf("Tree S is subtree of tree T");elseprintf("Tree S is not a subtree of tree T");getchar();return 0;}轉載于:https://my.oschina.net/zshuangyan/blog/173232
總結
以上是生活随笔為你收集整理的给定两个二叉树T和S,判断S是否为T的子树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [原创]互联网金融App测试介绍
- 下一篇: 恢复Ext3下被删除的文件