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