/ * Title: determining whether a given symmetrical binary tree. * / / * Ideas: 1, the recursive method. 2, basic conditions: two trees are empty is true; a null tree, a tree is not null, is false; two different values of the root of the tree, to false. 3, further: determining a left subtree of the tree and the tree right subtree 2 is symmetrical, it is determined whether the right sub-tree 1 and tree 2 child left symmetrically. * / #Include <the iostream> #include <string.h> #include <algorithm> #include <the cmath> #include <stdio.h> the using namespace STD; struct BinaryTreeNode { int Val; BinaryTreeNode * left; BinaryTreeNode * right; BinaryTreeNode (int X): Val (X), left (NULL), right (NULL) { } }; BOOL isSymmetrical (BinaryTreeNode * pRoot1, BinaryTreeNode pRoot2 *) { IF (pRoot1 && pRoot2 == == nullptr a nullptr a) { return to true; } if(pRoot1 == nullptr || pRoot2 == nullptr) return false; bool flag = false; if(pRoot1->val == pRoot2->val){ flag = isSymmetrical(pRoot1->left,pRoot2->right) && isSymmetrical(pRoot1->right,pRoot2->left); } return flag; } bool isSymmetrical(BinaryTreeNode* pRoot){ if(pRoot == nullptr) return false; return isSymmetrical(pRoot->left,pRoot->right); } int main(){ BinaryTreeNode* node1 = new BinaryTreeNode(8); BinaryTreeNode* node2 = new BinaryTreeNode(6); BinaryTreeNode* node3 = new BinaryTreeNode(6); BinaryTreeNode* node4 = new BinaryTreeNode(5); BinaryTreeNode* node5 = new BinaryTreeNode(7); BinaryTreeNode* node6 = new BinaryTreeNode(7); BinaryTreeNode* node7 = new BinaryTreeNode(5); node1->left = node2; node1->right = node3; node2->left = node4; node2->right = node5; node3->left = node6; node3->right = node7; cout<<isSymmetrical(node1)<<endl; }