1 Title Description
Two binary inputs A, B, B is judged not substructure A's. (Ps: we agreed empty tree is not a tree any substructure)
2 ideas and methods
(1) to find the root node and the node B of the same in A
(2) after the traversal to find the corresponding position of the other nodes until the node B is traversed, are the same, then B is A's subtree
Node (3) corresponding to the position are not the same, and exits continue to search the same as the root node B in A node, repeat steps until any binary tree is empty exit
3 C ++ core code
3.1 recursive 1
. 1 / * 2 struct the TreeNode { . 3 int Val; . 4 struct the TreeNode * left; . 5 struct the TreeNode * right; . 6 the TreeNode (int X): . 7 Val (X), left (NULL), right (NULL) { . 8 } . 9 } ; * / 10 class Solution { . 11 public : 12 is BOOL HasSubtree (* pRoot1 the TreeNode, the TreeNode * pRoot2) 13 is { 14 // 1. first find the same node and child root node 15 BOOL in Flag = to false ; 16 IF((pRoot1! = NULL) && (pRoot2! = NULL)) . 17 { 18 is IF (pRoot1-> Val pRoot2- ==> Val) . 19 // start determination, and at this time to find the subtree root node of the same node 20 = In Flag HasSubtreetty (pRoot1, pRoot2); 21 is 22 is IF (! In Flag) 23 is 24 In Flag = HasSubtree (pRoot1-> left, pRoot2); 25 26 is IF (! In Flag) 27 28 In Flag = HasSubtree (pRoot1-> right, pRoot2) ; 29 } 30 return In Flag; 31 is } 32 33 is BOOL HasSubtreetty (* pRoot1 the TreeNode, the TreeNode * pRoot2) 34 is { 35 // This function requires determination and after finding the same sub-root node of the node, the node determines whether the same rest 36 IF (pRoot2 == NULL) 37 [ return to true ; 38 is IF (! (pRoot1 == NULL) && (pRoot2 = NULL)) 39 return to false ; 40 IF (pRoot1-> Val = pRoot2->! Val) 41 is return to false ; 42 is return HasSubtreetty (pRoot1-> left, pRoot2-> left) && HasSubtreetty (pRoot1-> right, pRoot2-> right); 43 is } 44 };
3.2 recursive 2
1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 };*/ 10 class Solution { 11 public: 12 bool IsSubtree(TreeNode* p1, TreeNode* p2) 13 { 14 if(p2==NULL)return 1; 15 if(p1==NULL)return 0; 16 17 if(p1->val != p2->val)return 0; 18 19 return IsSubtree(p1->left,p2->left) && IsSubtree(p1->right,p2->right); 20 } 21 bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) 22 { 23 if(pRoot1==NULL || pRoot2==NULL)return 0; 24 25 return IsSubtree(pRoot1,pRoot2)|| 26 HasSubtree(pRoot1->left,pRoot2) || 27 HasSubtree(pRoot1->right,pRoot2); 28 } 29 };
4 complete code
1 #include <iostream> 2 3 using namespace std; 4 5 6 //struct ListNode { 7 // int val; 8 // struct ListNode *next; 9 // ListNode(int x) : val(x), next(NULL) {} 10 //}; 11 12 struct TreeNode { 13 int val; 14 struct TreeNode *left; 15 struct TreeNode *right; 16 TreeNode(int x) : 17 Val (X), left (NULL), right (NULL) { 18 is } . 19 }; 20 is 21 is class Solution { 22 is public : 23 is BOOL HasSubtree (* pRoot1 the TreeNode, the TreeNode * pRoot2) 24 { 25 // 1. First sub found and the same root node junction 26 is BOOL in Flag = to false ; 27 IF (!! (pRoot1 = NULL) && (pRoot2 = NULL)) 28 { 29 IF (pRoot1-> Val pRoot2- ==> Val) 30 // start determining, at this time, and found the child nodes of the same root node 31 = In Flag HasSubtreetty (pRoot1, pRoot2); 32 33 is IF (! In Flag) 34 is 35 In Flag = HasSubtree (pRoot1-> left, pRoot2); 36 37 [ IF (! In Flag) 38 is 39 In Flag = HasSubtree (pRoot1-> right, pRoot2) ; 40 } 41 is return in Flag; 42 is } 43 is 44 is BOOL HasSubtreetty (* pRoot1 the TreeNode, the TreeNode * pRoot2) 45 { 46 is // this function requires determination and after finding the same sub-root node of the node, the node determines whether the same remaining 47 if (pRoot2 == NULL) 48 return true; 49 if ((pRoot1 == NULL) && (pRoot2 != NULL)) 50 return false; 51 if (pRoot1->val != pRoot2->val) 52 return false; 53 return HasSubtreetty(pRoot1->left, pRoot2->left) && HasSubtreetty(pRoot1->right, pRoot2->right); 54 } 55 }; 56 57 int main() 58 { 59 Solution *s = new Solution(); 60 TreeNode *t1 = new TreeNode(8); 61 TreeNode *t2 = new TreeNode(9); 62 TreeNode *t3 = new TreeNode(3); 63 TreeNode *t4 = new TreeNode(8); 64 TreeNode *t5 = new TreeNode(2); 65 TreeNode *t6 = new TreeNode(4); 66 TreeNode *t7 = new TreeNode(7); 67 TreeNode *t8 = new TreeNode(6); 68 TreeNode *t9 = new TreeNode(5); 69 t1->left = t2; t1->right = t3; 70 t2->left = t4; t2->right = t5; t3->left = t6; t3->right = t7; 71 t4->left = t8; t4->right = t9; 72 73 TreeNode *tt1 = new TreeNode(8); //只有8 6相同时也是子树,返回值为1 74 //TreeNode *tt2 = new TreeNode(6); 75 //TreeNode *tt3 = new TreeNode(5); 76 TreeNode *tt2 = new TreeNode(1); 77 TreeNode *tt3 = new TreeNode(4); 78 tt1->left = tt2; tt1->right == tt3; 79 80 bool out_tree = s->HasSubtree(t1, tt1); 81 cout << out_tree << endl; 82 83 system("pause"); 84 return 0; 85 }
Reference material
https://blog.csdn.net/weixin_36125166/article/details/75939373
https://blog.csdn.net/danxibaoxxx/article/details/93402407