标签:结构 判断 roo val div turn esc 剑指offer 返回
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 private: 12 bool Equal(double num1, double num2) { 13 double subtract = num1 - num2; 14 return (subtract > -1e-8 && subtract < 1e-8); 15 } 16 bool DoesTree1HasTree2(TreeNode* pRoot1, TreeNode* pRoot2) { 17 //考虑是否先判断pRoot1 == NULL ? 18 //不能先简单判断pRoot1 == NULL就返回false, 因为可能pRoot2也为NULL,这是true 19 if (pRoot2 == NULL) 20 return true; 21 if (pRoot1 == NULL) 22 return false; 23 if (!Equal(pRoot1->val, pRoot2->val)) 24 return false; 25 return DoesTree1HasTree2(pRoot1->left, pRoot2->left) && DoesTree1HasTree2(pRoot1->right, pRoot2->right); 26 } 27 public: 28 bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) 29 { 30 bool flag = false; 31 if (pRoot1 != NULL && pRoot2 != NULL) { 32 if (Equal(pRoot1->val, pRoot2->val)) { 33 flag = DoesTree1HasTree2(pRoot1, pRoot2); 34 } 35 if (!flag) { 36 flag = HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2); 37 } 38 } 39 return flag; 40 } 41 };
标签:结构 判断 roo val div turn esc 剑指offer 返回
原文地址:https://www.cnblogs.com/qinduanyinghua/p/11288239.html