-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0117-PopulatingNextRightPointersInEachNode2.cs
47 lines (42 loc) · 1.35 KB
/
0117-PopulatingNextRightPointersInEachNode2.cs
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
//-----------------------------------------------------------------------------
// Runtime: 244ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0117_PopulatingNextRightPointersInEachNode2
{
public TreeLinkNode Connect(TreeLinkNode root)
{
TreeLinkNode cur = root;
while (cur != null)
{
TreeLinkNode nextHead = null;
TreeLinkNode nextPrevious = null;
while (cur != null)
{
if (cur.left != null)
{
if (nextPrevious != null)
nextPrevious.next = cur.left;
else
nextHead = cur.left;
nextPrevious = cur.left;
}
if (cur.right != null)
{
if (nextPrevious != null)
nextPrevious.next = cur.right;
else
nextHead = cur.right;
nextPrevious = cur.right;
}
cur = cur.next;
}
cur = nextHead;
}
return root;
}
}
}