二叉搜索树与双向链表

题目:输入一棵二叉树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。

 public Node Convert(Node node){
        Stack<Node> stack = new Stack<Node>();
        if(node == null) return null;
        Node old = null;
        Node head = null;
        while(true){
            while(node!=null){
                stack.push(node);
                node = node.left;
            }
            if(stack.isEmpty()) break;
            node = stack.pop();
            if(head == null){
                head = node;
            }
            if(old!=null){
                old.right = node;
                node.left = old;
            }
            old = node;
            node = node.right;
        }
        return head;
    }

 

posted @ 2018-07-09 23:46  樱圃  阅读(89)  评论(0编辑  收藏  举报