Time limit: C/C++ 1 second, other languages 2 seconds Space limit: C/C++ 64M, other languages 128M Hot index: 514583
Knowledge points of this question: Divide and conquer
Title description
Enter a binary search tree and 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.
ps: The solution to this problem has a new node, which does not meet the problem.
Idea: In-order traversal, left, middle and right, the right subtree of the node node pointing to the node points to the current recursive node pRootOfTree, the left subtree of pRootOfTree points to the node node, and then the node node moves backward, which is equal to node = pRootOfTree
function Convert(pRootOfTree){
let node = null,realHead = null
CovertSub(pRootOfTree)
return realHead
function CovertSub(pRootOfTree){
if(!pRootOfTree) return null
CovertSub(pRootOfTree.left)
if(!node){
node = pRootOfTree
realHead = pRootOfTree
}else{
node.right = pRootOfTree
pRootOfTree.left = node
node = pRootOfTree
}
CovertSub(pRootOfTree.right)
}
}