[劍指offer] JAVA版題解 面試題36

在這裏插入圖片描述

代碼演示:

package swordfingeroffer;

/**
 * <p>Description: </p>
 *
 * @author 羅志遠
 * @version 1.0
 * @name InterviewQuestion36
 * @date 2020-06-15 0:06
 */
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);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章