题目描述
- 请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
算法分析
- 递归,左子树和右子树对比,右子树和左子树对比;
提交代码(原始代码):
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot)
{
if (!pRoot)
return true;
return isSymmetricalCore(pRoot->left, pRoot->right);
}
bool isSymmetricalCore(TreeNode* pLeft, TreeNode* pRight)
{
if (!pLeft && !pRight)
return true;
if (pLeft && !pRight)
return false;
if (!pLeft && pRight)
return false;
bool result = false;
if (pLeft->val == pRight->val)
{
result = isSymmetricalCore(pLeft->left, pRight->right)
&& isSymmetricalCore(pLeft->right, pRight->left);
}
return result;
}
};
提交代码(合并冗余代码):
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot)
{
return isSymmetricalCore(pRoot, pRoot);
}
bool isSymmetricalCore(TreeNode* pLeft, TreeNode* pRight)
{
if (!pLeft && !pRight)
return true;
if (!pLeft || !pRight)
return false;
if (pLeft->val != pRight->val)
return false;
return isSymmetricalCore(pLeft->left, pRight->right)
&& isSymmetricalCore(pLeft->right, pRight->left);
}
};
测试代码:
// ====================测试代码====================
void Test(char* testName, TreeNode* pRoot, bool expected)
{
if (testName != nullptr)
printf("%s begins: ", testName);
Solution s;
if (s.isSymmetrical(pRoot) == expected)
printf("Passed.\n");
else
printf("FAILED.\n");
}
// 8
// 6 6
// 5 7 7 5
void Test1()
{
TreeNode* pNode8 = CreateBinaryTreeNode(8);
TreeNode* pNode61 = CreateBinaryTreeNode(6);
TreeNode* pNode62 = CreateBinaryTreeNode(6);
TreeNode* pNode51 = CreateBinaryTreeNode(5);
TreeNode* pNode71 = CreateBinaryTreeNode(7);
TreeNode* pNode72 = CreateBinaryTreeNode(7);
TreeNode* pNode52 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode8, pNode61, pNode62);
ConnectTreeNodes(pNode61, pNode51, pNode71);
ConnectTreeNodes(pNode62, pNode72, pNode52);
Test("Test1", pNode8, true);
DestroyTree(pNode8);
}
// 8
// 6 9
// 5 7 7 5
void Test2()
{
TreeNode* pNode8 = CreateBinaryTreeNode(8);
TreeNode* pNode61 = CreateBinaryTreeNode(6);
TreeNode* pNode9 = CreateBinaryTreeNode(9);
TreeNode* pNode51 = CreateBinaryTreeNode(5);
TreeNode* pNode71 = CreateBinaryTreeNode(7);
TreeNode* pNode72 = CreateBinaryTreeNode(7);
TreeNode* pNode52 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode8, pNode61, pNode9);
ConnectTreeNodes(pNode61, pNode51, pNode71);
ConnectTreeNodes(pNode9, pNode72, pNode52);
Test("Test2", pNode8, false);
DestroyTree(pNode8);
}
// 8
// 6 6
// 5 7 7
void Test3()
{
TreeNode* pNode8 = CreateBinaryTreeNode(8);
TreeNode* pNode61 = CreateBinaryTreeNode(6);
TreeNode* pNode62 = CreateBinaryTreeNode(6);
TreeNode* pNode51 = CreateBinaryTreeNode(5);
TreeNode* pNode71 = CreateBinaryTreeNode(7);
TreeNode* pNode72 = CreateBinaryTreeNode(7);
ConnectTreeNodes(pNode8, pNode61, pNode62);
ConnectTreeNodes(pNode61, pNode51, pNode71);
ConnectTreeNodes(pNode62, pNode72, nullptr);
Test("Test3", pNode8, false);
DestroyTree(pNode8);
}
// 5
// / \
// 3 3
// / \
// 4 4
// / \
// 2 2
// / \
// 1 1
void Test4()
{
TreeNode* pNode5 = CreateBinaryTreeNode(5);
TreeNode* pNode31 = CreateBinaryTreeNode(3);
TreeNode* pNode32 = CreateBinaryTreeNode(3);
TreeNode* pNode41 = CreateBinaryTreeNode(4);
TreeNode* pNode42 = CreateBinaryTreeNode(4);
TreeNode* pNode21 = CreateBinaryTreeNode(2);
TreeNode* pNode22 = CreateBinaryTreeNode(2);
TreeNode* pNode11 = CreateBinaryTreeNode(1);
TreeNode* pNode12 = CreateBinaryTreeNode(1);
ConnectTreeNodes(pNode5, pNode31, pNode32);
ConnectTreeNodes(pNode31, pNode41, nullptr);
ConnectTreeNodes(pNode32, nullptr, pNode42);
ConnectTreeNodes(pNode41, pNode21, nullptr);
ConnectTreeNodes(pNode42, nullptr, pNode22);
ConnectTreeNodes(pNode21, pNode11, nullptr);
ConnectTreeNodes(pNode22, nullptr, pNode12);
Test("Test4", pNode5, true);
DestroyTree(pNode5);
}
// 5
// / \
// 3 3
// / \
// 4 4
// / \
// 6 2
// / \
// 1 1
void Test5()
{
TreeNode* pNode5 = CreateBinaryTreeNode(5);
TreeNode* pNode31 = CreateBinaryTreeNode(3);
TreeNode* pNode32 = CreateBinaryTreeNode(3);
TreeNode* pNode41 = CreateBinaryTreeNode(4);
TreeNode* pNode42 = CreateBinaryTreeNode(4);
TreeNode* pNode6 = CreateBinaryTreeNode(6);
TreeNode* pNode22 = CreateBinaryTreeNode(2);
TreeNode* pNode11 = CreateBinaryTreeNode(1);
TreeNode* pNode12 = CreateBinaryTreeNode(1);
ConnectTreeNodes(pNode5, pNode31, pNode32);
ConnectTreeNodes(pNode31, pNode41, nullptr);
ConnectTreeNodes(pNode32, nullptr, pNode42);
ConnectTreeNodes(pNode41, pNode6, nullptr);
ConnectTreeNodes(pNode42, nullptr, pNode22);
ConnectTreeNodes(pNode6, pNode11, nullptr);
ConnectTreeNodes(pNode22, nullptr, pNode12);
Test("Test5", pNode5, false);
DestroyTree(pNode5);
}
// 5
// / \
// 3 3
// / \
// 4 4
// / \
// 2 2
// \
// 1
void Test6()
{
TreeNode* pNode5 = CreateBinaryTreeNode(5);
TreeNode* pNode31 = CreateBinaryTreeNode(3);
TreeNode* pNode32 = CreateBinaryTreeNode(3);
TreeNode* pNode41 = CreateBinaryTreeNode(4);
TreeNode* pNode42 = CreateBinaryTreeNode(4);
TreeNode* pNode21 = CreateBinaryTreeNode(2);
TreeNode* pNode22 = CreateBinaryTreeNode(2);
TreeNode* pNode12 = CreateBinaryTreeNode(1);
ConnectTreeNodes(pNode5, pNode31, pNode32);
ConnectTreeNodes(pNode31, pNode41, nullptr);
ConnectTreeNodes(pNode32, nullptr, pNode42);
ConnectTreeNodes(pNode41, pNode21, nullptr);
ConnectTreeNodes(pNode42, nullptr, pNode22);
ConnectTreeNodes(pNode21, nullptr, nullptr);
ConnectTreeNodes(pNode22, nullptr, pNode12);
Test("Test6", pNode5, false);
DestroyTree(pNode5);
}
// 只有一个结点
void Test7()
{
TreeNode* pNode1 = CreateBinaryTreeNode(1);
Test("Test7", pNode1, true);
DestroyTree(pNode1);
}
// 没有结点
void Test8()
{
Test("Test8", nullptr, true);
}
// 所有结点都有相同的值,树对称
// 5
// / \
// 5 5
// / \
// 5 5
// / \
// 5 5
void Test9()
{
TreeNode* pNode1 = CreateBinaryTreeNode(5);
TreeNode* pNode21 = CreateBinaryTreeNode(5);
TreeNode* pNode22 = CreateBinaryTreeNode(5);
TreeNode* pNode31 = CreateBinaryTreeNode(5);
TreeNode* pNode32 = CreateBinaryTreeNode(5);
TreeNode* pNode41 = CreateBinaryTreeNode(5);
TreeNode* pNode42 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode1, pNode21, pNode22);
ConnectTreeNodes(pNode21, pNode31, nullptr);
ConnectTreeNodes(pNode22, nullptr, pNode32);
ConnectTreeNodes(pNode31, pNode41, nullptr);
ConnectTreeNodes(pNode32, nullptr, pNode42);
ConnectTreeNodes(pNode41, nullptr, nullptr);
ConnectTreeNodes(pNode42, nullptr, nullptr);
Test("Test9", pNode1, true);
DestroyTree(pNode1);
}
// 所有结点都有相同的值,树不对称
// 5
// / \
// 5 5
// / \
// 5 5
// / /
// 5 5
void Test10()
{
TreeNode* pNode1 = CreateBinaryTreeNode(5);
TreeNode* pNode21 = CreateBinaryTreeNode(5);
TreeNode* pNode22 = CreateBinaryTreeNode(5);
TreeNode* pNode31 = CreateBinaryTreeNode(5);
TreeNode* pNode32 = CreateBinaryTreeNode(5);
TreeNode* pNode41 = CreateBinaryTreeNode(5);
TreeNode* pNode42 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode1, pNode21, pNode22);
ConnectTreeNodes(pNode21, pNode31, nullptr);
ConnectTreeNodes(pNode22, nullptr, pNode32);
ConnectTreeNodes(pNode31, pNode41, nullptr);
ConnectTreeNodes(pNode32, pNode42, nullptr);
ConnectTreeNodes(pNode41, nullptr, nullptr);
ConnectTreeNodes(pNode42, nullptr, nullptr);
Test("Test10", pNode1, false);
DestroyTree(pNode1);
}
void main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
Test10();
}