-
Notifications
You must be signed in to change notification settings - Fork 22
/
connect nodes at same level
85 lines (75 loc) · 2.52 KB
/
connect nodes at same level
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Given a binary tree, connect the nodes that are at same level.
Structure of the given Binary Tree node is like following.
struct Node
{
int data;
Node* left;
Node* right;
Node* nextRight;
}
Initially, all the nextRight pointers point to garbage values. Your function should set these pointers to point next right for each node.
10 10 ------> NULL
/ \ / \
3 5 => 3 ------> 5 --------> NULL
/ \ \ / \ \
4 1 2 4 --> 1 -----> 2 -------> NULL
Input Format:
The input contains T, denoting number of testcases. For each testcase there will be third lines. The first line contains number of edges.
The second line contains relation between nodes. The second line contains two nodes and a character separated by space. The first node
denotes data value, second node denotes where it will be assigned to the previous node which will depend on character 'L' or 'R' i.e.
the 2nd node will be assigned as left child to the 1st node if character is 'L' and so on. The first node of second line is root node.
The struct or class Node has a data part which stores the data, pointer to left child and pointer to right child. There are multiple
test cases. For each test case, the function will be called individually.
Output Format:
For each testcase, in a new line, print the level order and inorder traversals.
Your Task:
You don't have to take input. Complete the function connect() that takes node as parameter and connects the nodes at same level.
The printing is done by the driver code.
Constraints:
1 <= T <= 100
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000
Example:
Input:
2
2
3 1 L 3 2 R
4
10 20 L 10 30 R 20 40 L 20 60 R
Output:
3 1 2
1 3 2
10 20 30 40 60
40 20 60 10 30
Explanation:
Testcase1: The connected tree is
3 ------> NULL
/ \
1 -----> 2 ------ NULL
Testcase2: The connected tree is
10 ----------> NULL
/ \
20 ------> 30 -------> NULL
/ \
40 ----> 60 ----------> NULL
*/
void connect(Node *p)
{
queue<Node *> q;
q.push(p);
q.push(NULL);
while(!q.empty()){
Node *temp = q.front();
q.pop();
if(temp==NULL){
if(!q.empty())
q.push(NULL);
}
else{
temp->nextRight = q.front();
if(temp->left) q.push(temp->left);
if(temp->right) q.push(temp->right);
}
}
}