|
| 1 | +package binarySearchTree.assignment; |
| 2 | + |
| 3 | +import binarySearchTree.TreeNode; |
| 4 | + |
| 5 | +import static binarySearchTree.SearchInBST.levelWiseInput; |
| 6 | +/* |
| 7 | +Given a BST, convert it into a sorted linked list. You have to return the head of LL. |
| 8 | +Input format: |
| 9 | +The first and only line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. |
| 10 | +Output Format: |
| 11 | +The first and only line of output prints the elements of sorted linked list. |
| 12 | +Constraints: |
| 13 | +Time Limit: 1 second |
| 14 | +Sample Input 1: |
| 15 | +8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 |
| 16 | +Sample Output 1: |
| 17 | +2 5 6 7 8 10 |
| 18 | +*/ |
| 19 | + |
| 20 | +class ListNode<T> { |
| 21 | + T data; |
| 22 | + ListNode<T> next; |
| 23 | + |
| 24 | + public ListNode(T data) { |
| 25 | + this.data = data; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class PairListNode { |
| 30 | + ListNode<Integer> head; |
| 31 | + ListNode<Integer> tail; |
| 32 | +} |
| 33 | + |
| 34 | +public class BinarySearchTreeToLinkedList { |
| 35 | + |
| 36 | + public static ListNode<Integer> constructLinkedList(TreeNode<Integer> root) { |
| 37 | + return helper(root).head; |
| 38 | + } |
| 39 | + |
| 40 | + private static PairListNode helper(TreeNode<Integer> root) { |
| 41 | + if (root == null) return new PairListNode(); |
| 42 | + |
| 43 | + ListNode<Integer> newNode = new ListNode<>(root.data); |
| 44 | + |
| 45 | + var left = helper(root.left); |
| 46 | + var right = helper(root.right); |
| 47 | + |
| 48 | + PairListNode pair = new PairListNode(); |
| 49 | + |
| 50 | + if (left.tail != null) { |
| 51 | + left.tail.next = newNode; |
| 52 | + } |
| 53 | + |
| 54 | + newNode.next = right.head; |
| 55 | + |
| 56 | + if (left.tail != null) { |
| 57 | + pair.head = left.head; |
| 58 | + } else { |
| 59 | + pair.head = newNode; |
| 60 | + } |
| 61 | + |
| 62 | + if (right.tail != null) { |
| 63 | + pair.tail = right.tail; |
| 64 | + } else { |
| 65 | + pair.tail = newNode; |
| 66 | + } |
| 67 | + |
| 68 | + return pair; |
| 69 | + } |
| 70 | + |
| 71 | + private static void printListNode(ListNode<Integer> head) { |
| 72 | + System.out.print("[ "); |
| 73 | + while (head != null) { |
| 74 | + System.out.print(head.data + " -> "); |
| 75 | + head = head.next; |
| 76 | + } |
| 77 | + System.out.println("null ]"); |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(String[] args) { |
| 81 | + var root = levelWiseInput(); |
| 82 | + |
| 83 | + System.out.println("Constructing the Linked List..."); |
| 84 | + var head = constructLinkedList(root); |
| 85 | + |
| 86 | + System.out.println("The Linked List is:"); |
| 87 | + printListNode(head); |
| 88 | + } |
| 89 | +} |
0 commit comments