-
Notifications
You must be signed in to change notification settings - Fork 1
/
populating child O(1)
59 lines (55 loc) · 1.32 KB
/
populating child O(1)
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
class connectnode{
static iNode root;
connectnode()
{
root=null;
}
iNode getnextright(iNode node)
{
iNode temp = node.nextright;
while(temp!=null)
{
if(temp.left!=null)
return temp.left;
if(temp.right!=null)
return temp.right;
temp = temp.nextright;
}
return null;
}
void connection(iNode node)
{
if(node==null)
{
return ;
}
node.nextright=null;
while(node!=null)
{
iNode n = node;
while(n!=null)
{
if(n.left!=null)
{
if(n.right!=null){
n.left.nextright = n.right;
}
else{
n.left.nextright=getnextright(n);
}
}
if(n.right!=null)
{
n.right.nextright = getnextright(n);
}
n = n.nextright;
}
if(node.left!=null)
node = node.left;
if(node.right!=null)
node = node.right;
else
node = getnextright(node);
}
}
}