题解 | #二叉树的下一个结点#

二叉树的下一个结点

https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e

/*function TreeLinkNode(x){
    this.val = x;
    this.left = null;
    this.right = null;
    this.next = null;
}*/

function inOrder(node, results) {
    if (node !== null) {
        inOrder(node.left, results);
        results.push(node);
        inOrder(node.right, results);
    }
}

function GetNext(pNode) {
    var root = pNode;
    while (root.next !== null) {
        root = root.next;
    }
    var results = [];
    inOrder(root, results);
    let index = results.indexOf(pNode)
    return results[index+1]
}

module.exports = {
    GetNext: GetNext,
};

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务