I don't know what's problem althought it gives the wrong testing case


  • 0
    R
    void connect(TreeLinkNode *root)
    {
    	if(root==NULL) return;
    	vector<TreeLinkNode* > vec;
    	vec.push_back(root);
    	int current=0;
    	TreeLinkNode* lastp;
    	while(current<vec.size())
    	{
    		int last=vec.size();//the final one's next one
    		int i=0;
    		while(current<last)
    		{
    			TreeLinkNode* p=vec.at(current);
    			if(i>0)
    				lastp->next=p;//level first one, do not have last
    
    			lastp=p;
    			cout<<p->val;
    			if(p->left)
    				vec.push_back(p->left);
    			if(p->right)
    				vec.push_back(p->right);
    			current++;
    			i++;
    		}
    		lastp->next=NULL;//level last next is null
    		cout<<endl;
    	}
    }
    

    The feedback is wrong anwser:
    Input: {0}
    Output: 0
    Expected: {0,#}
    but I don't know why if it is a root, output {0,#} instead of {0}, also I do not need to populate the last pointer of each level. Maybe I think # means a null pointer, and maybe {0 # #would be the right output} I think. This a level traverse algorithm for a BT, it's from beauty of progtramming. But when I see that initial is NULL, it does not need to separate each level, for assigning last pointer of each level as NULL.


  • 0
    S

    Remove all occurrences of 'cout's and you'll be fine.


  • 0
    R

    Thanks. What a stupid mistake that I made? I always focus on important isssue and ignore some simple things.


Log in to reply
 

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