Cattle customer - symmetrical binary tree

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
Here Insert Picture Description

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;
    }
};
Published 315 original articles · won praise 119 · views 110 000 +

Guess you like

Origin blog.csdn.net/w144215160044/article/details/104919555