My C++ Non Recursive AC Solution


  • 0
    C
    class Solution {
    public:
        bool isSymmetric(TreeNode *root) {
            
            if (root == NULL){
                return true;
            }
            
            if ((root->left == NULL && root->right != NULL) || (root->left != NULL && root->right == NULL)){
                return false;
            }
            
            vector<TreeNode *> deq;
            TreeNode * p = NULL;
            int rear = 0;
            int front = 0;
            int level = 0;
            deq.push_back(root);
            rear++;
            level = rear;
            int i, j;
            
            while (front < rear){
                p = deq[front];
                front++;
                if (p->left != NULL){
                    deq.push_back(p->left);
                    rear++;
                }
                if (p->right != NULL){
                    deq.push_back(p->right);
                    rear++;
                }
                if (front == level){
                    level = rear;
                    if ((rear - front) & 1){ 
                        return false;
                    }        
                    
                    for(i = front, j = rear - 1; i < j; i++, j--){
                        if((deq[i]->left != NULL && deq[j]->right == NULL) || (deq[i]->left == NULL && deq[j]->right != NULL) || 
                        (deq[i]->val != deq[j]->val)){
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    };

  • 0
    S

    Buddy, It's iterative method.


Log in to reply
 

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