題目:給定一個二叉樹和其中的一個結點,請找出中序遍歷順序的下一個結點并且回傳,注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指標,

/*function TreeLinkNode(x){ this.val = x; this.left = null; this.right = null; this.next = null; }*/ function GetNext(pNode) { if (pNode === null) { return null; } if (pNode.right !== null) { // 第1種 pNode = pNode.right; while (pNode.left !== null) { pNode = pNode.left; } return pNode; } while (pNode.next !== null) { // 第2種 if (pNode === pNode.next.left) { return pNode.next; } pNode = pNode.next; } return null; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/24668.html
標籤:其他
上一篇:阿里一面電話面
下一篇:Git 查看某個檔案的修改歷史
