bool isSymmetrical(TreeNode* r1, TreeNode* r2) { if (r1 == NULL && r2 == NULL) return true; if (r1 == NULL || r2 == NULL) return false; if (r1->val != r2->val) return false; return isSymmetrical(r1->left, r2->right) && isSymmetrical(r1->right, r2->left); }
bool isSymmetrical(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) { if(pRoot1 == NULL && pRoot2 == NULL) return true; if(pRoot1 == NULL || pRoot2 == NULL) return false; if(pRoot1->m_nValue != pRoot2->m_nValue) return false; return isSymmetrical(pRoot1->m_pLeft, pRoot2->m_pRight) && isSymmetrical(pRoot1->m_pRight, pRoot2->m_pLeft); }
int main(void) { char str[1001]; while (gets(str)) { isSymmetrical(str); } return 0; }
// ==================== Test Code ==================== void Test(char* testName, BinaryTreeNode* pRoot, bool expected) { if(testName != NULL) printf("%s begins: ", testName); if(isSymmetrical(pRoot) == expected) printf("Passed.\n"); else printf("FAILED.\n"); }
bool isSymmetrical(BinaryTreeNode* pRoot) { return isSymmetrical(pRoot, pRoot); }
bool isSymmetrical(TreeNode* pRoot) { if (pRoot == NULL) return true; return isSymmetrical(pRoot->left, pRoot->right); }
void problem3_17(){ bool answer = isSymmetrical('&', std::cin, '@'); printBool(answer); }