My c++ 4ms solution, how to improve?


  • 0
    1
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            if(!root) return true;
            if(!root->left&&!root->right) return true;
            else if(!root->left||!root->right) return false;
            else if(root->left->val!=root->right->val) return false;
            TreeNode* temp=new TreeNode(0);
            temp->left=root->left->left;
            temp->right=root->right->right;
            bool flag1=isSymmetric(temp);
            temp->left=root->left->right;
            temp->right=root->right->left;
            bool flag2=isSymmetric(temp);
            delete temp;
            if(flag1&&flag2) return true;
            return false;
            
        }
    };

Log in to reply
 

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