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

二叉搜索树与双向链表

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

全部评论

相关推荐

本神尊:看来是没招到小红薯上的人
点赞 评论 收藏
分享
06-02 15:53
阳光学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
投递长鑫存储等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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