剑指offer刷题之c++实现的树的子结构

来源:互联网 发布:魔兽数据库安卓 编辑:程序博客网 时间:2024/06/07 02:16

参考文档:

c语言中判断一个字符串是否包含另一个字符串

深入分析C++中char* str和char str[]的区别

C++及C中的 string char指针及char数组

思路:

遍历一棵树,花费O(n)的时间复杂度。

判断一个字符串是否是另一个字符串的子串,可以使用c中的库函数 char *strstr(char *str1, char *str2); 。


 #include "myHead.h" #include "allConstructBinaryTree.cpp" #include <string.h>/*输入两颗二叉树A,B,判断B是不是A的子结构。*/  void inOrder(TreeNode *h,vector<int> &res);  TreeNode* createBinaryTree1();  TreeNode* createBinaryTree2();  bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2){    if(pRoot2 == NULL)return false;if(pRoot1 ==NULL)return false;vector<int> res1,res2;string s1="",s2="";constchar *c1,*c2;char t ='0'; inOrder(pRoot1,res1);for(vector<int>::iterator it=res1.begin();it<res1.end();++it){  t=(*it)+t;  s1=s1+t;  t='0';}inOrder(pRoot2,res2); t='0';for(vector<int>::iterator it=res2.begin();it<res2.end();++it){  t=(*it)+t;  s2=s2+t;  t='0';}//将string类型的转化为char*来实现自定义的操作,C++标准库也为了和之前用C写的程序兼容,于是可以用string的c_str()函数。//string var = "Olympic";//char *ptr = var.c_str(); //还不能被编译//但是c_str()为了防止意外地修改string对象,返回的是const指针,所以上面这段代码是不能被编译的。正确的应该是用const指针。//string var = "Olympic";//const char *p = var.c_str(); //Correct!c1 = s1.c_str();c2 = s2.c_str();return strstr(c1,c2);} void inOrder(TreeNode *h,vector<int> &res){  if(h){      res.push_back(h->val);     inOrder(h->left,res);// cout<<h->val; inOrder(h->right,res);  } } TreeNode* createBinaryTree1(){//    TreeNode *T = (TreeNode *) malloc(sizeof(TreeNode)) ;//T->val = 8;//  T->left = (TreeNode *) malloc(sizeof(TreeNode)) ;//  T->left->val = 8;//    T->left->left = (TreeNode *) malloc(sizeof(TreeNode)) ;//T->left->left->val = 9;//T->left->left->left = T->left->left->right = NULL;//T->left->right = (TreeNode *) malloc(sizeof(TreeNode)) ;//T->left->right->val = 2;//T->left->right->left = (TreeNode *) malloc(sizeof(TreeNode)) ;//T->left->right->left->val = 4;//T->left->right->left->left=T->left->right->left->right =NULL;//T->left->right->right = (TreeNode *) malloc(sizeof(TreeNode)) ;//T->left->right->right->val = 7;//T->left->right->right->left =T->left->right->right->right = NULL;//  T->right = (TreeNode *) malloc(sizeof(TreeNode)) ;//  T->right->val = 7;//   T->right->left = T->right->right=NULL ;TreeNode *T = (TreeNode *) malloc(sizeof(TreeNode)) ;T->val = 8;  T->left = (TreeNode *) malloc(sizeof(TreeNode)) ;  T->left->val = 9;    T->left->left =  T->left->right = NULL;  T->right = (TreeNode *) malloc(sizeof(TreeNode)) ;  T->right->val = 3;   T->right->left == (TreeNode *) malloc(sizeof(TreeNode)) ;  T->right->left->val = 2;    T->right->right = NULL; return T;    } TreeNode* createBinaryTree2(){    TreeNode *T = (TreeNode *) malloc(sizeof(TreeNode)) ;T->val = 8;  T->left = (TreeNode *) malloc(sizeof(TreeNode)) ;  T->left->val = 9;    T->left->left =  T->left->right = NULL;  T->right = (TreeNode *) malloc(sizeof(TreeNode)) ;  T->right->val = 2;   T->right->left = T->right->right = NULL;return T;    }  int main(){ TreeNode* h1 =createBinaryTree1(); TreeNode* h2 =NULL;//=createBinaryTree2(); HasSubtree(h1,h2); } 


0 0