剑指offer之 二叉搜索树与双向链表

Posted toov5

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑指offer之 二叉搜索树与双向链表相关的知识,希望对你有一定的参考价值。

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

public class Solution {
    public TreeNode Convert(TreeNode pRootOfTree) {
        if (pRootOfTree == null)
            return pRootOfTree;
        //TreeNode pLastOfList = null;
        TreeNode pLastOfList = new TreeNode(0);
        pLastOfList = ConvertNode(pRootOfTree, pLastOfList);
        TreeNode pHeadOfList = pLastOfList;
        while (pLastOfList!=null  && pHeadOfList.left!=null) {
            pHeadOfList = pHeadOfList.left;
        }
        pHeadOfList = pHeadOfList.right;
        pHeadOfList.left = null;
        return pHeadOfList;
    }

    public TreeNode ConvertNode (TreeNode pNode, TreeNode pLastOfList) {
        if (pNode == null) 
            return pNode;
        TreeNode pCurrent = pNode;
        if (pCurrent.left != null) 
            pLastOfList = ConvertNode(pCurrent.left, pLastOfList);
        pCurrent.left = pLastOfList;
        if (pLastOfList != null)
            pLastOfList.right = pCurrent;
        pLastOfList = pCurrent;
        if (pCurrent.right != null) {
            pLastOfList = ConvertNode(pCurrent.right, pLastOfList);
        }
        return pLastOfList;
    }

  

以上是关于剑指offer之 二叉搜索树与双向链表的主要内容,如果未能解决你的问题,请参考以下文章

Java 剑指offer(36) 二叉搜索树与双向链表

剑指 Offer 36. 二叉搜索树与双向链表

剑指Offer-二叉搜索树与双向链表

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

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

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