Convert a binary tree into a two-way linked list
Recursive solution:
(1) If the binary tree search tree is empty, no conversion is required. The first node of the two-way linked list is NULL, And the last node is NULL.
(2) If the binary search tree is not empty:
If the left subtree is empty, the first node corresponding to the bidirectional ordered linked list is the root node, and no other operations are required on the left;
If the left subtree is not empty, convert the left subtree. The first node of the Binary Search Tree corresponds to the bidirectional ordered linked list is the first node of the bidirectional ordered linked list after the left subtree is converted, connect the root node to the last node of the bidirectional ordered chain table converted from the left subtree;
If the right subtree is empty, the last node corresponding to the bidirectional ordered linked list is the root node, and no other operations are required on the right;
If the right subtree is not empty, the last node corresponding to the bidirectional ordered linked list is the last node of the bidirectional ordered linked list after the right subtree conversion, connect the first node of the bidirectional ordered linked list converted from the root node to the right subtree.
The reference code is as follows:
/*************************************** * ************************************ Parameters: pRoot: Binary query tree root node pointer pFirstNode: The first node pointer to the bidirectional ordered linked list after conversion pLastNode: ****************************** **************************************** * ******/void Convert (BinaryTreeNode * pRoot, optional * & pFirstNode, BinaryTreeNode * & pLastNode) {BinaryTreeNode * pFirstLeft, * pLastLeft, * pFirstRight, * pLastRight; if (pRoot = NULL) {pFirstNode = NULL; pLastNode = NULL; return;} if (pRoot-> m_pLeft = NULL) {// if the left subtree is empty, the first node corresponding to the bidirectional ordered linked list is the root node pFirstNode = pRoot ;} else {Convert (pRoot-> m_pLeft, pFirstLeft, pLastLeft ); // The first node of the bidirectional ordered linked list corresponding to the binary search tree is pFirstNode = pFirstLeft, the first node of the bidirectional ordered linked list after the left subtree conversion; // connect the last node of the bidirectional ordered linked list converted from the root node and the left subtree to pRoot-> m_pLeft = pLastLeft; pLastLeft-> m_pRight = pRoot ;} if (pRoot-> m_pRight = NULL) {// The Last node corresponding to the bidirectional ordered linked list is the root node pLastNode = pRoot;} else {Convert (pRoot-> m_pRight, pFirstRight, pLastRight); // The Last node corresponding to the bidirectional ordered linked list is the last node of the bidirectional ordered linked list after the right subtree conversion. pLastNode = pLastRight; // connect the first node of the bidirectional ordered linked list converted from the root node and the right subtree to pRoot-> m_pRight = pFirstRight; pFirstRight-> m_pRight = pRoot;} return ;}