Title Description
Please implement a function, a binary tree is used to determine not symmetrical. Note that if a binary image is a binary tree with this same definition as symmetrical.
Solution:
1, if the root node is empty, it must be symmetrical
2, shown below, satisfy the binary symmetric, mirror images of the same color
class Solution {
public:
bool isequal(TreeNode* leftRoot, TreeNode* rightRoot)
{
if (leftRoot==NULL && rightRoot==NULL)
return true;
else if (leftRoot==NULL || rightRoot==NULL)
return false;
if (leftRoot->val == rightRoot->val)
return isequal(leftRoot->left, rightRoot->right) && isequal(leftRoot->right, rightRoot->left);
else
return false;
}
bool isSymmetrical(TreeNode* pRoot)
{
if (pRoot==NULL || isequal(pRoot->left, pRoot->right))
return true;
else
return false;
}
};