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

二叉搜索树与双向链表

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

全部评论

相关推荐

头像
04-29 10:53
已编辑
东北大学 自动化类
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务