题解 | #二叉树的下一个结点#
二叉树的下一个结点
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
/*function TreeLinkNode(x){ this.val = x; this.left = null; this.right = null; this.next = null; }*/ var res = []; function midOrder(root){ if(root == null){ return null; } if(root.left != null){ midOrder(root.left); } res.push(root); if(root.right != null){ midOrder(root.right); } } function GetNext(pNode) { // write code here //先记录要寻找的节点 let node = pNode; //找到树的根节点 while(pNode.next != null){ pNode = pNode.next; } let lastRes = null; midOrder(pNode); //循环遍历数组找 要寻找的节点 for(let i = 0;i < res.length-1;i++){ if(res[i].val == node.val){ lastRes = res[i+1]; break; } } //处理边界情况节点 if( res[res.length-1].val == node.val && node.next == null){ lastRes = null; } return lastRes; } module.exports = { GetNext : GetNext };#我的实习求职记录#