题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
//设置一个前驱节点用于当前节点left连接 , 一个头节点用于最后返回链表
var pre = null;
var head = null;
function Convert(pRootOfTree)
{
// write code here
if(pRootOfTree == null){
return null;
}
//中序遍历:左 中 右
Convert(pRootOfTree.left);
// let node = pRootOfTree;
// if(nodeLeft != null)
// node.left = nodeLeft;
// if(nodeRight != null)
// node.right = nodeRight;
//
if(pre == null){
pre = pRootOfTree;
head = pRootOfTree;
}
else{
pre.right = pRootOfTree;
pRootOfTree.left = pre;
pre = pRootOfTree;
}
Convert(pRootOfTree.right);
return head;
}
module.exports = {
Convert : Convert
};
#我的实习求职记录#