"Symmetrical Binary Tree" of "Swords Offer"

Topic description


Please implement a function to determine whether a binary tree is symmetric. Note that a binary tree is defined as symmetric if it is the same as its mirror image.

Code


/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    bool isSymmetrical(TreeNode* pRoot)
    {
        if(pRoot == nullptr){
            return true;
        }
        return find(pRoot ->left,pRoot->right);
    }
    bool find(TreeNode*left ,TreeNode* right){
        if(left == nullptr){
            return right == nullptr;
        }
        if(right == nullptr){
            return false;
        }
        if(left->val != right->val){
            return false;
        }
       return find(left->right,right->left) && find(left-> left ,right->right);
    }

};


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325609350&siteId=291194637