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

二叉搜索树与双向链表

https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5

JS版本

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function inorder(arr, pRootOfTree) {
    if (pRootOfTree === null) return;
    inorder(arr, pRootOfTree.left);
    arr.push(pRootOfTree);
    inorder(arr, pRootOfTree.right);
}

function Convert(pRootOfTree) {
    // write code here
    
    let arr = []
    inorder(arr, 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]
}
module.exports = {
    Convert: Convert,
};

全部评论

相关推荐

09-28 22:01
已编辑
广西科技大学 IT技术支持
合适才能收到offe...:找桌面运维?
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
08-20 19:41
那一天的Java_J...:简历完全流水账,学生思维很严重,还有很大的优化空间,可以多看看牛客的简历。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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