题解 | #二叉树的下一个结点#
二叉树的下一个结点
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, };