码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-10-13 12:26:06      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:div   搜索   class   logs   log   turn   blog   val   list   

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之 二叉搜索树与双向链表

标签:div   搜索   class   logs   log   turn   blog   val   list   

原文地址:http://www.cnblogs.com/toov5/p/7660071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!