## Topic description

Input a binary search tree, convert the binary search tree into a sorted doubly linked list. It is required that no new nodes can be created, and only the point of the node pointer in the tree can be adjusted.

topic analysis

In a binary tree, each node has two pointers to child nodes. In a doubly linked list, each node also has two pointers, which point to the previous node and the next node respectively. According to the value of the left child node is always less than the value of the parent node, the value of the right child node is always greater than the value of the parent node. Traverse the nodes of the tree using inorder. There are two methods recursive non-recursive.

```/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
TreeNode *pre=nullptr;
return convert(pRootOfTree,&pre);
}
TreeNode* convert(TreeNode* pRootOfTree,TreeNode **pre)
{
if(pRootOfTree==nullptr)
return nullptr;
auto ret=convert(pRootOfTree->left,pre);
if(ret==nullptr)
ret = pRootOfTree;
if(*pre!=nullptr)
(*pre)->right=pRootOfTree;
pRootOfTree->left=*pre;
convert (pRootOfTree-> right, (* pre = pRootOfTree, pre));
return ret;
}
};```
```Method 1: Non-recursive version
Problem solving ideas:
1. The core is a non-recursive algorithm for in-order traversal.
2. Modify the pointers of the current traversed node and the previous traversed node.
import java.util.Stack;
public TreeNode ConvertBSTToBiList(TreeNode root) {
if(root==null)
return null;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root;
TreeNode pre = null;// The previous node used to save the in-order traversal sequence
boolean isFirst = true;
while(p!=null||!stack.isEmpty()){
while(p!=null){
stack.push(p);
p = p.left;
}
p = stack.pop();
if(isFirst){
root = p;// record the first node in the in-order traversal sequence as root
pre = root;
isFirst = false;
}else{
pre.right = p;
p.left = pre;
pre = p;
}
p = p.right;
}
return root;
}

1.将左子树构造成双链表，并返回链表头节点。
2.定位至左子树双链表最后一个节点。
3.如果左子树链表不为空的话，将当前root追加到左子树链表。
4.将右子树构造成双链表，并返回链表头节点。
5.如果右子树链表不为空的话，将该链表追加到root节点之后。
6.根据左子树链表是否为空确定返回的节点。
public TreeNode Convert(TreeNode root) {
if(root==null)
return null;
if(root.left==null&&root.right==null)
return root;
// 1.将左子树构造成双链表，并返回链表头节点
TreeNode left = Convert(root.left);
TreeNode p = left;
// 2.定位至左子树双链表最后一个节点
while(p!=null&&p.right!=null){
p = p.right;
}
// 3.如果左子树链表不为空的话，将当前root追加到左子树链表
if(left!=null){
p.right = root;
root.left = p;
}
// 4.将右子树构造成双链表，并返回链表头节点
TreeNode right = Convert(root.right);
// 5.如果右子树链表不为空的话，将该链表追加到root节点之后
if(right!=null){
right.left = root;
root.right = right;
}
return left!=null?left:root;
}

// 记录子树链表的最后一个节点，终结点只可能为只含左子树的非叶节点与叶节点
protected TreeNode leftLast = null;
public TreeNode Convert(TreeNode root) {
if(root==null)
return null;
if(root.left==null&&root.right==null){
leftLast = root;// 最后的一个节点可能为最右侧的叶节点
return root;
}
// 1.将左子树构造成双链表，并返回链表头节点
TreeNode left = Convert(root.left);
// 3.如果左子树链表不为空的话，将当前root追加到左子树链表
if(left!=null){
leftLast.right = root;
root.left = leftLast;
}
leftLast = root;// 当根节点只含左子树时，则该根节点为最后一个节点
// 4.将右子树构造成双链表，并返回链表头节点
TreeNode right = Convert(root.right);
// 5.如果右子树链表不为空的话，将该链表追加到root节点之后
if(right!=null){
right.left = root;
root.right = right;
}
return left!=null?left:root;
}
```

### Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324638380&siteId=291194637
Recommended
Ranking
Daily