Does any one use queue?Is my code too stupid?


  • -3
    W
    class Solution {
    public:
        typedef struct QueueNode
        {
            TreeLinkNode *data[10000];
            int fristindex,lastindex;
            QueueNode():fristindex(0),lastindex(0){}
        }QueueNode;
        void Inqueue(QueueNode &T,TreeLinkNode * node)
        {
            if(T.lastindex<=10000)
            {
            T.data[T.lastindex] = node;
            T.lastindex++;
            }
        }
        TreeLinkNode * Outqueue(QueueNode &T)
        {
            TreeLinkNode *fristnode=NULL;
            if(T.fristindex>=0)
            {
            fristnode=T.data[T.fristindex];
            T.fristindex++;
            }
            return fristnode;
        }
        bool IsQueueEmpty(QueueNode T)
        {
            if(T.lastindex==T.fristindex)
                return true;
            else
                return false;
        }
        void Inqueueleftright(QueueNode &T,TreeLinkNode *root)
        {
            if(NULL!=root->left)
            {
               Inqueue(T,root->left);
               Inqueue(T,root->right);
            }
        }
        void connect(TreeLinkNode *root) {
            QueueNode queue;
            TreeLinkNode *T=root;
            Inqueue(queue,T);
            int i=1;
            if(NULL==root)
                return;
            while(!(IsQueueEmpty(queue)))
            {
                TreeLinkNode *temp1 =Outqueue(queue);
                Inqueueleftright(queue,temp1);
                for(int j=0;j<pow(2,i-1)-1;j++)
                {
                    temp1 ->next = Outqueue(queue);
                    Inqueueleftright(queue,temp1 ->next);
                    temp1 = temp1 ->next;
                }
                temp1 ->next = NULL;
                i++;
            }
    
        }
    };

  • 0
    W

    It's accepted but costs 104ms.


Log in to reply
 

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