美文网首页
Java日记2018-05-11

Java日记2018-05-11

作者: hayes0420 | 来源:发表于2018-05-11 07:33 被阅读0次

    第一题 二叉搜索树与双向链表

    https://www.cnblogs.com/edisonchou/p/4793345.html
    解题思路待补充

    package com.lyc.dataautest;
    
    public class Convert {
        public static BinaryTreeNode Convert(BinaryTreeNode root) {
            BinaryTreeNode last = null;
            convertnode(root,last);
            
            //找到头结点并返回
            BinaryTreeNode headInList = last;
            while (headInList != null && headInList.left != null)
            {
                headInList = headInList.left;
            }
    
            return headInList;
            
        }
        public static void convertnode(BinaryTreeNode root,BinaryTreeNode lastnode) {
            if(root==null) return;
            BinaryTreeNode current = root;
            //左子树的递归
            if(current.left!=null) {
                convertnode(current.left,lastnode);
            }
            //左子树建立双向链表
            current.left=lastnode;
            if(lastnode!=null) {
                lastnode.right=current;
            }
            //指针迁移,并递归右子树
            lastnode = current;
            if(current.right!=null) {
                convertnode(current.right,lastnode);
            }
            
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:Java日记2018-05-11

          本文链接:https://www.haomeiwen.com/subject/xeuxdftx.html