/* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function inorder(pRootOfTree) { // 使用中序遍历 if (!pRootOfTree) return [null, null]; let [leftH, leftT] = inorder(pRootOfTree.left); if (leftT) { leftT.right = pRootOfTree; pRootOfTree.left = leftT; // 将左子树的末尾与当前节点头相连接 } let [rightH, rightT] = inorder(pRootOfTree.right); if (rightH) { pRootOfTree.right = rightH; rightH.left = pRootOfTree; // 将右子树的开头与当前节点尾相连接 } return [leftH ? leftH : pRootOfTree, rightT ? rightT : pRootOfTree]; // 返回的结果永远是树的最开头和树的最结尾,如果为空,则是当前的节点进行替换 } function Convert(pRootOfTree) { [leftH, rightT] = inorder(pRootOfTree); console.log(leftH, rightT); return leftH; } module.exports = { Convert: Convert, };