Memory Limit Exceeded for Iterative solution


#1
int Solution::isSymmetric(TreeNode* A) {
    if (A) {
        TreeNode* left = A->left;
        TreeNode* right = A->right;
        
        if (!left || !right) {
            return left == right;
        }
        
        while (left && right) {
            
            if (left->val != right->val) {
                return 0;
            }
            
            TreeNode* t1 = left;
            TreeNode* t2 = right;
            
            while (t1 && t2) {
                
                // cout << t1->val << " " << t2->val << endl;
                
                if (t1->val == t2->val) {
                    return 0;
                } else {
                    t1 = t1->left;
                    t2 = t2->right;
                }
            }
            
            if (t1 || t2) {
                return 0;
            }
    
            left = left->right;
            right = right->left;

        }
        
        return left == right;
    }
    
    return 1;
}