题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Convert(pRootOfTree)
{
// write code here
if (pRootOfTree === null) return null
const array = []
inOrder(pRootOfTree, array)
for (let i = 0; i < array.length - 1; i++) {
array[i].right = array[i + 1]
array[i + 1].left = array[i]
}
return array[0]
}
function inOrder(root, array) {
if (root === null) return
inOrder(root.left, array)
array.push(root)
inOrder(root.right, array)
}
module.exports = {
Convert : Convert
};
