剑指offer27:二叉搜索树与双向链表

来源:互联网 发布:剑灵saber捏脸数据 编辑:程序博客网 时间:2024/05/17 02:43
/** * Created by WHZ on 2017/4/5. */public class offer27 {    private class BinaryTreeNode{        int val;        BinaryTreeNode left;        BinaryTreeNode right;    }    BinaryTreeNode Convert(BinaryTreeNode pRootOfTree){        BinaryTreeNode pLastNodeInList = ConvertNode(pRootOfTree);        BinaryTreeNode pHeadOfList = pLastNodeInList;        while(pHeadOfList!=null&&pHeadOfList.left!=null)            pHeadOfList = pHeadOfList.left;        return pHeadOfList;    }    private BinaryTreeNode ConvertNode(BinaryTreeNode pNode) {        if(pNode == null)            return null;        BinaryTreeNode pLastNodeInList = null;        if(pNode.left !=null)            pLastNodeInList = ConvertNode(pNode.left);        pNode.left = pLastNodeInList;        if(pLastNodeInList !=null)            pLastNodeInList.right = pNode;        pLastNodeInList = pNode;        if(pNode.right!=null)            pLastNodeInList = ConvertNode(pNode.right);        return pLastNodeInList;    }}

0 0