标签:style class blog code java http
题目:输入两棵二叉树A和B,判断B是不是A的子结构。
1 #include<iostream> 2 using namespace std; 3 4 struct BinaryTreeNode { 5 int m_nValue; 6 BinaryTreeNode* m_pLeft; 7 BinaryTreeNode* m_pRight; 8 } 9 10 bool DoesTree1HasTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) { 11 if(pRoot2 == NULL) 12 return true; 13 if(pRoot1 == NULL) 14 return false; 15 16 if(pRoot2->m_nValue == pRoot1->m_nValue) 17 return false; 18 return DoesTree1HasTree2(pRoot1->m_pLeft, pRoot2->m_pLeft) 19 && DoesTree1HasTree2(pRoot1->m_pRight, pRoot2->m_pRight); 20 } 21 22 bool HasSubTree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) { 23 bool result = false; 24 25 if(pRoot1 != NULL && pRoot2 != NULL) { 26 if(pRoot2->m_nValue == pRoot1->m_nValue) 27 result = DoesTree1HasTree2(pRoot1, pRoot2); 28 if(!result) 29 result = HasSubTree(pRoot1->m_pLeft, pRoot2); 30 if(!result) 31 result = HasSubTree(pRoot1->m_pRight, pRoot21); 32 } 33 } 34 35 int main() 36 { 37 38 return 0; 39 }
标签:style class blog code java http
原文地址:http://www.cnblogs.com/wanghui390/p/3772954.html