Convert a binary search tree into a sorted two-way linked list

Source: Internet
Author: User

Sequential Traversal

Void convertnode (bstreenode * pnode, bstreenode * & plastnodeinlist)
{
If (pnode = NULL)
Return;

Bstreenode * pcurrent = pnode;

// Convert the left sub-tree
If (pcurrent-> m_pleft! = NULL)
Convertnode (pcurrent-> m_pleft, plastnodeinlist );

// Put the current node into the double-linked list
Pcurrent-> m_pleft = plastnodeinlist;
If (plastnodeinlist! = NULL)
Plastnodeinlist-> m_pright = pcurrent;

Plastnodeinlist = pcurrent;

// Convert the right sub-tree
If (pcurrent-> m_pright! = NULL)
Convertnode (pcurrent-> m_pright, plastnodeinlist );
}

//////////////////////////////////////// ///////////////////////////////
// Covert a binary search tree into a sorted double-linked list
// Input: pheadoftree-the head of tree
// Output: The head of sorted double-linked list
//////////////////////////////////////// ///////////////////////////////
Bstreenode * convert_solution1 (bstreenode * pheadoftree)
{
Bstreenode * plastnodeinlist = NULL;
Convertnode (pheadoftree, plastnodeinlist );

// Get the head of the double-linked list
Bstreenode * pheadoflist = plastnodeinlist;
While (pheadoflist & pheadoflist-> m_pleft)
Pheadoflist = pheadoflist-> m_pleft;

Return pheadoflist;
}

Convert a binary search tree into a sorted two-way linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.