# JZ36 Binary Search Tree and Doubly Linked List (Niuke.com)

Input a binary search tree and convert the binary search tree into a sorted doubly linked list. As shown in the figure below

Note:
1. It is required that no new nodes can be created, only the pointing of the node pointer in the tree can be adjusted. After the conversion is completed, the left pointer of the node in the tree needs to point to the predecessor, and the right pointer of the node in the tree needs to point to the successor 2. Return the pointer of the
first node in the linked list
3. The TreeNode returned by the function has left and right pointers, which can be seen in fact Form a data structure of a doubly linked list
4. You don’t need to output the doubly linked list, the program will automatically print out according to your return value

Idea, in a binary tree, each node has two pointers to child nodes. In the doubly linked list, each node also has two pointers, pointing to the previous node and the next node respectively. The structure of these two nodes is very similar. The binary search tree is a sorted data structure. The value of its left child node is always less than the value of the parent node, and the value of the right child node is always greater than the value of the parent node.

The original pointer to the left child node is adjusted to the pointer to the previous node in the linked list.
The original pointer to the right child node is adjusted to the pointer to the next node in the linked list.

Let's use a specific example for further analysis. When we perform in-order traversal to the root node, we can think of the tree as three parts: 1. The node with a value of 10; 2. The left subtree with a root node value of 6; 3. The root The right subtree with a node value of 14

According to the definition of the sorted linked list, a node with a value of 10 will be linked with the largest node in its left subtree (the node with a value of 8), and it will also be linked with the smallest node in the right subtree The node (the node with a value of 12) is linked, as shown in the figure below, it is split into the root node, left and right subtrees, we convert the left subtree and the right subtree into a doubly linked list and then combine with the root node Linked, the entire binary search tree is converted into a sorted doubly linked list.

According to the order of in-order traversal, when we traverse to the root node (the node with a value of 10), its left subtree has been converted into a sorted linked list, and the current value is the largest when it is the last node in the linked list of nodes. We link the node with a value of 8 to the root node, and the last node in the linked list is now 10. Then we traverse the converted right subtree and link the root node with the smallest node in the right subtree.
The conversion process of the left and right subtree nodes is the same as the traversal process, so we can use recursion to solve the problem.

code:

``````class Solution {

public:
void Inorder(TreeNode* cur,TreeNode*& prev)  //采用应用可以当前节点的上一个节点
{

//判空，如果为空则不需要链接
if(cur==nullptr)return;
Inorder(cur->left,prev);
//当前left指向前一个
cur->left=prev;
//当前right指向后一个
if(prev)prev->right=cur;
//更新prev
prev=cur;
Inorder(cur->right,prev);
}
TreeNode* Convert(TreeNode* pRootOfTree) {

TreeNode* prev=nullptr;
Inorder(pRootOfTree,prev);
TreeNode* ret=pRootOfTree;
//要返回的节点值为最小值的节点，也就是二叉排序树的最左节点
while(ret&& ret->left)ret=ret->left;
return ret;
}
};
``````

Recursively expand Figure 6 as an example of the root node

# Sword refers to Offer 36. Binary search tree and doubly linked list

The idea is the same as above, since this question needs to establish a two-way circular linked list, so we only need to find the leftmost node and the rightmost node of the binary tree search tree for head-to-tail interconnection, because the leftmost node of the binary search tree is the smallest in the binary search tree element, the rightmost node of the binary search tree is the largest node of the binary search tree.

code:

``````class Solution {

public:
void Inorder(Node* cur,Node*& prev)
{

if(cur==nullptr)return;

Inorder(cur->left,prev);
//当前left指向前一个
cur->left=prev;
//当前right指向后一个
if(prev)prev->right=cur;
//更新prev
prev=cur;
Inorder(cur->right,prev);
}
Node* treeToDoublyList(Node* root) {

if(root==NULL)return NULL;
Node* prev=nullptr;
Inorder(root,prev);
//首位互连部分代码
Node* _left=root;
while(_left&& _left->left)_left=_left->left;  //找到搜索二叉树的最左节点，就是链表的头节点
Node* _right=root;
while(_right&&_right->right)_right=_right->right; //找到搜索二叉树的最右节点，就是链表的尾节点
//头节点的left指向尾节点，尾节点的right节点指向头节点
_left->left=_right;
_right->right=_left;
return _left;
}
};
``````

### Guess you like

Origin blog.csdn.net/Tianzhenchuan/article/details/132080981
Recommended
Ranking
Daily