题解 | #二叉树的下一个结点#
二叉树的下一个结点
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
/*function TreeLinkNode(x){ this.val = x; this.left = null; this.right = null; this.next = null; }*/ function midOrderRead(node, results) { if (node === null) return midOrderRead(node.left, results) results.push(node) midOrderRead(node.right, results) } function GetNext(pNode) { // write code here if (pNode === null) return null let root = pNode while (root.next !== null) { root = root.next } const results = [] midOrderRead(root, results) for (let i = 0; i < results.length; i++) { if (results[i].val === pNode.val && i < results.length - 1) { return results[i + 1] } } return null } module.exports = { GetNext : GetNext };