Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 90cda11

Browse files
Create BST to LL
1 parent 80d9fc6 commit 90cda11

File tree

1 file changed

+104
-0
lines changed
  • Course 2 - Data Structures in JAVA/Lecture 13 - BST I

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Given a BST, convert it into a sorted linked list. You have to return the head of LL.
3+
4+
Input format:
5+
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.
6+
7+
Output Format:
8+
The first and only line of output prints the elements of sorted linked list.
9+
10+
Constraints:
11+
Time Limit: 1 second
12+
13+
Sample Input 1:
14+
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
15+
Sample Output 1:
16+
2 5 6 7 8 10
17+
*/
18+
class PassNode<T>
19+
{
20+
T head;
21+
T tail;
22+
}
23+
public class Solution {
24+
25+
/*
26+
* Binary Tree Node class
27+
*
28+
* class BinaryTreeNode<T> { T data; BinaryTreeNode<T> left; BinaryTreeNode<T>
29+
* right;
30+
*
31+
* public BinaryTreeNode(T data) { this.data = data; } }
32+
*/
33+
34+
/*
35+
* LinkedList Node Class
36+
*
37+
*
38+
* class LinkedListNode<T> { T data; LinkedListNode<T> next;
39+
*
40+
* public LinkedListNode(T data) { this.data = data; } }
41+
*/
42+
43+
public static LinkedListNode<Integer> constructLinkedList(BinaryTreeNode<Integer> root) {
44+
PassNode<LinkedListNode<Integer>> passNode= constructLinkedListHelper(root);
45+
return passNode.head;
46+
}
47+
48+
public static PassNode<LinkedListNode<Integer>> constructLinkedListHelper(BinaryTreeNode<Integer> root)
49+
{
50+
if(root==null)
51+
{
52+
return new PassNode<LinkedListNode<Integer>>();
53+
}
54+
55+
if (root.left==null && root.right==null)
56+
{
57+
PassNode<LinkedListNode<Integer>> passNode = new PassNode<LinkedListNode<Integer>>();
58+
passNode.head=new LinkedListNode<Integer>(root.data);
59+
passNode.tail=null;
60+
return passNode;
61+
}
62+
63+
PassNode<LinkedListNode<Integer>> passNodeLeft = constructLinkedListHelper(root.left);
64+
PassNode<LinkedListNode<Integer>> passNodeRight = constructLinkedListHelper(root.right);
65+
PassNode<LinkedListNode<Integer>> passNode = new PassNode<LinkedListNode<Integer>>();
66+
LinkedListNode<Integer> newNode=new LinkedListNode<Integer>(root.data);
67+
68+
if (passNodeLeft.head==null)
69+
{
70+
passNode.head=newNode;
71+
//passNode.tail=newNode;
72+
}
73+
else
74+
{
75+
passNode.head=passNodeLeft.head;
76+
}
77+
78+
if(passNodeLeft.tail==null)
79+
{
80+
passNode.head.next=newNode;
81+
passNode.tail=newNode;
82+
}
83+
else
84+
{
85+
passNodeLeft.tail.next=newNode;
86+
passNode.tail=newNode;
87+
}
88+
89+
if (passNodeRight.head!=null)
90+
{
91+
passNode.tail.next=passNodeRight.head;
92+
passNode.tail=passNodeRight.head;
93+
}
94+
95+
if(passNodeRight.tail!=null)
96+
{
97+
passNode.tail=passNodeRight.tail;
98+
}
99+
100+
return passNode;
101+
102+
}
103+
}
104+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /