将二叉搜索树转换成一个排序的双向链表

class Node {
    char val;
    Node left;
    Node right;
    Node(char val) {
        this.val = val;
    }
}
public class Tree {
public Node Convert(Node pRootOfTree) {
        if (pRootOfTree == null) {
            return null;
        }
        convertChild(pRootOfTree);
        Node head = pRootOfTree;
        while (head.left != null) {
            head = head.left;
        }
        return head;
    }

Node prev = null;
public void convertChild(Node root) {
        if (root == null) {
            return ;
        }

        convertChild(root.left);
        root.left = prev;
        if (prev != null) {
            prev.right = root;
        }
        prev = root;
        convertChild(root.right);
    }
//打印单链表
    public void display(Node head) {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.right;
        }
        System.out.println();
    }
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页