Share my recursive C++ solution,easy to understand


  • 6
    V
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            if (root == NULL)
                return true;
            
            return checkSymmetric(root->left, root->right);
        }
        //check the two nodes in symmetric position
        bool checkSymmetric(TreeNode *leftSymmetricNode, TreeNode *rightSymmetricNode)
        {
            if (leftSymmetricNode == NULL && rightSymmetricNode == NULL)
                return true;
            if (leftSymmetricNode == NULL || rightSymmetricNode == NULL)
                return false;
            if (leftSymmetricNode->val == rightSymmetricNode->val)
                return checkSymmetric(leftSymmetricNode->left, rightSymmetricNode->right) && checkSymmetric(leftSymmetricNode->right, rightSymmetricNode->left);
            return false;
        }
    };

  • 0
    P

    great solution..!!!!..really easy to understand....


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.