【问题标题】:Sorted Singly linked list to BST in place将单链表排序到 BST
【发布时间】:2016-03-03 18:06:05
【问题描述】:

我参考了许多帖子和答案,但仍然无法获得有效的代码。下面是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

任何帮助将不胜感激。

【问题讨论】:

    标签: java data-structures binary-search-tree recursive-datastructures


    【解决方案1】:

    请在函数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;
      
      }
      

    【讨论】:

    • 太棒了!!解决方案有效...你能解释一下这背后的逻辑 if (first.next != null) { first.data = first.next.data; first.next = first.next.next;想知道为什么这不起作用 if (first.next != null) { first= first.next; }
    • 对最近发布的问题有何见解?
    【解决方案2】:

    您没有指定需要平衡 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;
         }
     }
    

    【讨论】:

      【解决方案3】:

      大家好,问题是将 bst 转换为 java 中的排序单链表

      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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 2019-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多