劍指offer面試題27:二叉搜索樹與雙向鏈表

題目:輸入一顆二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。要求不能建立任何新的節點,只能調整樹中節點指針的指向。node

因爲二叉搜索樹是有序的,左子結點的值小於根節點的值,右子結點的值大於根節點的值。因此在把二叉搜索樹轉換成排序的雙向鏈表的時候要把左子樹中的最大值的右子樹指針指向根節點,把右子樹中的最小值的左子樹指針指向根節點。this

因爲先訪問根節點,所以要用中序遍歷的方式進行處理。spa

package Solution;

public class No27ConvertBinarySearchTreeToLinkedList {

    static class BinaryTreeNode {
        int value;
        BinaryTreeNode left;
        BinaryTreeNode right;

        public BinaryTreeNode() {

        }

        public BinaryTreeNode(int value, BinaryTreeNode left,
                BinaryTreeNode right) {
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    public static BinaryTreeNode convert(BinaryTreeNode root) {
        BinaryTreeNode lastNodeInList = null;
        lastNodeInList = convertToNode(root, lastNodeInList);
        BinaryTreeNode head = lastNodeInList;
        // 從尾節點返回頭結點
        while (head != null && head.left != null) {
            head = head.left;
        }
        printList(head);
        return head;
    }

    private static BinaryTreeNode convertToNode(BinaryTreeNode node,
            BinaryTreeNode lastNodeInList) {
        if (node == null)
            return null;
        BinaryTreeNode current = node;
        // 遞歸的處理左子樹
        if (current.left != null)
            lastNodeInList = convertToNode(current.left, lastNodeInList);
        // 使鏈表中的最後一個結點指向左子樹的最小的節點
        current.left = lastNodeInList;
        // 鏈表中的最後一個結點指向當前節點,當前節點就成了鏈表中的最後一個結點
        if (lastNodeInList != null)
            lastNodeInList.right = current;
        lastNodeInList = current;
        // 遞歸轉換右子樹
        if (current.right != null)
            lastNodeInList = convertToNode(current.right, lastNodeInList);

        return lastNodeInList;
    }

    public static void printList(BinaryTreeNode head) {
        while (head != null) {
            System.out.print(head.value + ",");
            head = head.right;
        }
    }

    // 中序遍歷二叉樹
    public static void printTree(BinaryTreeNode root) {
        if (root != null) {
            printTree(root.left);
            System.out.print(root.value + ",");
            printTree(root.right);
        }
    }

    public static void main(String[] args) {
        BinaryTreeNode node1 = new BinaryTreeNode();
        BinaryTreeNode node2 = new BinaryTreeNode();
        BinaryTreeNode node3 = new BinaryTreeNode();
        BinaryTreeNode node4 = new BinaryTreeNode();
        BinaryTreeNode node5 = new BinaryTreeNode();
        BinaryTreeNode node6 = new BinaryTreeNode();
        BinaryTreeNode node7 = new BinaryTreeNode();
        node7.value = 16;
        node6.value = 12;
        node5.value = 14;
        node5.left = node6;
        node5.right = node7;
        node3.value = 4;
        node4.value = 8;
        node2.value = 6;
        node2.left = node3;
        node2.right = node4;
        node1.value = 10;
        node1.left = node2;
        node1.right = node5;
        printTree(node1);
        System.out.println();
        System.out.println("=============打印鏈表================");
        convert(node1);
    }
}
相關文章
相關標籤/搜索