剑指 Offer 26. 树的子结构
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * struct TreeNode *left; 6 * struct TreeNode *right; 7 * }; 8 */ 9 10 bool isSubStructure2(struct TreeNode* A, struct TreeNode* B) { 11 if (B == NULL) 12 return true; 13 if (A == NULL) 14 return false; 15 if (A->val != B->val) 16 return false; 17 return isSubStructure2(A->left, B->left) && isSubStructure2(A->right, B->right); 18 } 19 20 bool isSubStructure(struct TreeNode* A, struct TreeNode* B){ 21 bool result =false; 22 if (A && B) { 23 if (A->val == B->val) { 24 result = isSubStructure2(A, B); 25 } 26 if (result == false) { 27 result = isSubStructure(A->left, B); 28 } 29 if (result == false) 30 result = isSubStructure(A->right, B); 31 } 32 return result; 33 }