String the leaf nodes of the binary tree into a linked list

Using the right pointer of the node, the leaf node of a binary tree inherits a singly linked list from left to right.

void link(BTNode* p,BTNode* head,BETNode* tail)
{
    
    
	if(p)
	{
    
    
		
		if(!p->left&&!p->right)
		{
    
    
			
			if(head==NULL)
		    {
    
    
    			head=p;
    			tail=p;
    		}
    		else
    		{
    
    
		    	tail->next=p;
		    	tail=p;
		    }
		}
		link(p->left,head,tail);
		link(p->right,head,tail);
	}
} 

Guess you like

Origin blog.csdn.net/Cxf2018/article/details/105553202