簡體   English   中英

將單鏈表排序到 BST 就位

[英]Sorted Singly linked list to BST in place

我已經參考了許多帖子和答案,但仍然無法獲得工作代碼。 下面是在java中排序鏈表到BST的代碼。 包括鏈表的所有輔助函數。

我得到的輸出不是預期的,即 root : 4 , root.left 是 2 , root.right 再次是 4 。 我想輸出應該是 root : 4 , root.left 是 2 而 root.right 是 6

class LNode {
    public int data;
    public LNode next;

    LNode(int newData) {
        this.data = newData;
    }
}

class Node {

    int data;
    Node left;
    Node right;
    public Node prev;

    Node(int d) {
        data = d;
        left = right = null;
    }
}

class LinkedList {
    LNode first;
    LNode head;
    LNode newNode;

    public LinkedList() {
        first = null;
    }

    public void insertAtBeginning(int x) {
        newNode = new LNode(x);
        if (first != null) {
            newNode.next = first;
            first = newNode;
            head = first;
        } else {
            first = newNode;
            head = first;
        }
    }

    public void printList()

    {
        head = first;
        while (first != null) {
            System.out.print(first.data + " --> ");
            first = first.next;
        }
        System.out.println("null");
        first = head;

    }
}

public class LLtoBST {
    public static Node root;
    //public static LNode first;

    public static Node sortedListToBST(LNode first, int end) {

        return sortedListToBST(first, 0, end);
    }

    public static Node sortedListToBST(LNode first, int start, int end) {

        if (start > end)
            return null;
        if (first != null) {
            int mid = start + (end - start) / 2;

            Node lnode = sortedListToBST(first, start, mid - 1);

            root = new Node(first.data);

            first = first.next;
            Node rnode = sortedListToBST(first, mid + 1, end);

            root.left = lnode;
            root.right = rnode;

        }
        return root;

    }

    public static void main(String... args) {
        LinkedList list = new LinkedList();
        int n = 0;
        list.insertAtBeginning(7);
        list.insertAtBeginning(6);
        list.insertAtBeginning(5);
        list.insertAtBeginning(4);
        list.insertAtBeginning(3);
        list.insertAtBeginning(2);
        list.insertAtBeginning(1);
        list.printList();

        first = list.head;

        while (first != null) {
            n++;
            first = first.next;
        }

        first = list.head;

        Node curr = sortedListToBST(first, n);
        System.out.println(curr.data);
        System.out.println(curr.left.data);
        System.out.println(curr.right.data);

    } 

}

Output :
1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> null
4
2
4

任何幫助將不勝感激。

請在sortedListToBST(LNode first, int start, int end)函數中進行以下更改

請注意,下面的代碼將 2 打印為 4 的左孩子,將 6 打印為 4 的右孩子。此外,我已經測試了以 2 和 6 為根的代碼。 它將 1 作為 2 的左孩子打印,3 作為 2 的右孩子打印。此外,5 作為 6 的左孩子和 7 作為 6 的右孩子。

另請注意,您需要更改您的測試客戶端,以便在左孩子和/或右孩子為空時處理用例。 我希望下面的代碼有幫助。

下面的算法基於樹的有序遍歷原理。

  1. 遍歷左子樹。
  2. 訪問根。
  3. 遍歷右子樹。

     public static Node sortedListToBST(LNode first, int start, int end) { if (start > end) return null; int mid = (start + end) / 2; Node leftNode = sortedListToBST(first, start, mid - 1); Node root = new Node(first.data); root.left = leftNode; if (first.next != null) { first.data = first.next.data; first.next = first.next.next; } root.right = sortedListToBST(first, mid + 1, end); return root; }

您沒有指定需要平衡的BST。 如果您對不平衡的 BST 感到滿意,則以下方法有效:

 public static Node sortedListToUnbalancedBST(LNode lNode) {
     if (lNode.next != null) {
         Node node = new Node(lNode.data);
         node.right = sortedListToUnbalancedBST(lNode.next);
         return node;
     } else {
        return null;
     }
 }

大家好,問題是在java中將bst轉換為排序的單向鏈表

class SortedLL {
    LinkedListNode<Integer> head;
    LinkedListNode<Integer> tail;
}

public class Solution {

    public static SortedLL sortedLL(BinaryTreeNode<Integer> root) {

        if (root == null) {
            SortedLL output = new SortedLL();
            output.head = null;
            output.tail = null;
            return output;
        }

        SortedLL l = sortedLL(root.left);
        SortedLL r = sortedLL(root.right);

        LinkedListNode<Integer> n = new LinkedListNode<Integer>(root.data);

        if (l.tail != null) {
            l.tail.next = n;
        }

        if (r.head != null) {
            n.next = r.head;
        }

        SortedLL b = new SortedLL();

        if (l.head != null) {
            b.head = l.head;
        } else {
            b.head = n;
        }

        if (r.tail != null) {
            b.tail = r.tail;
        } else {
            b.tail = n;
        }

        return b;
    }

    public static LinkedListNode<Integer> BSTToSortedLL(BinaryTreeNode<Integer> root) {
        return sortedLL(root).head;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM