-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathBTree-to-DLL.java
56 lines (45 loc) · 1.68 KB
/
BTree-to-DLL.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Binary Tree to DLL
Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must be same as Inorder of the given Binary Tree. The first node of Inorder traversal (leftmost node in BT) must be the head node of the DLL.
Input:
The first line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
Output:
For each test case, in a new line, print front to end and back to end traversals of DLL.
Your Task:
You don't have to take input. Complete the function bToDLL() that takes root node of the tree as a parameter and returns the head of DLL . The driver code prints the DLL both ways.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(H).
Note: H is the height of the tree and this space is used implicitly for recursion stack. */
class Node
{
Node left, right;
int data;
Node(int d)
{
data = d;
left = right = null;
}
}
// This function should convert a given Binary tree to Doubly
// Linked List
class Ishan
{
Node head;
Node prev=null;
Node bToDLL(Node root)
{
if(root==null)
return root;
bToDLL(root.left);
if(prev==null)
head=root;
else{
prev.right=root;
root.left=prev;
}
prev=root;
bToDLL(root.right);
return head;
}
}