题解 | #判断t1树中是否有与t2树拓扑结构完全相同的子树#
判断t1树中是否有与t2树拓扑结构完全相同的子树
http://www.nowcoder.com/practice/4eaccec5ee8f4fe8a4309463b807a542
递归
容我骂两句,智障拓扑结构完全相同需要值也相同???
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ bool isContains(TreeNode* root1, TreeNode* root2) { // write code here if(root1 == NULL && root2 == NULL) return true; if(root1 == NULL || root2 == NULL) return false; return isEqual(root1, root2) || isContains(root1 -> left, root2) || isContains(root1 -> right, root2); } bool isEqual(TreeNode* root1, TreeNode* root2){ if(root1 == NULL && root2 == NULL) return true; if(root1 == NULL || root2 == NULL) return false; if(root1 -> val != root2 -> val) return false; //妈的智障题 return isEqual(root1 -> left, root2 -> left) && isEqual(root1 -> right, root2 -> right); } };