![在这里插入图片描述](https://img-blog.csdnimg.cn/20200621095808178.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI4MjQ4Ng==,size_16,color_FFFFFF,t_70)
代码演示:
package swordfingeroffer;
public class InterviewQuestion36 {
public TreeNode convert(TreeNode pRootOfTree) {
TreeNode pLastNodeInList = null;
convertNode(pRootOfTree, pLastNodeInList);
TreeNode pHeadOfList = pLastNodeInList;
while (null != pHeadOfList && null != pHeadOfList.left) {
pHeadOfList = pHeadOfList.left;
}
return pHeadOfList;
}
private void convertNode(TreeNode pNode, TreeNode pLastNodeInList) {
if (null == pNode) {
return;
}
TreeNode pCurrent = pNode;
if (null != pCurrent.left) {
convertNode(pCurrent.left, pLastNodeInList);
}
pCurrent.left = pLastNodeInList;
if (null != pLastNodeInList) {
pLastNodeInList.right = pCurrent;
}
pLastNodeInList = pCurrent;
if (null != pCurrent.right) {
convertNode(pCurrent.right, pLastNodeInList);
}
}
}