题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

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
};

#我的实习求职记录#
全部评论

相关推荐

VirtualBool:都去逗他了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务