-
Notifications
You must be signed in to change notification settings - Fork 160
/
116_PopulatingNextRightPointersInEachNode.cs
73 lines (70 loc) · 2.12 KB
/
116_PopulatingNextRightPointersInEachNode.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeetCodeInCSharp
{
public class TreeLinkNode
{
public int val;
public TreeLinkNode left;
public TreeLinkNode right;
public TreeLinkNode next;
public TreeLinkNode(int val)
{
this.val = val;
left = right = next = null;
}
public override string ToString()
{
var list = new List<string>();
var queue = new Queue<TreeLinkNode>();
var temp = this;
queue.Enqueue(temp);
while (queue.Count != 0)
{
temp = queue.Dequeue();
list.Add(temp.val.ToString());
if (temp.next == null) list.Add("#");
if (temp.left != null) queue.Enqueue(temp.left);
if (temp.right != null) queue.Enqueue(temp.right);
}
return "[" + String.Join(",", list) + "]";
}
}
public class ConnectSolution
{
//BFS using queue
public static void ConnectQueue(TreeLinkNode root)
{
if (root == null) return;
var queue = new Queue<TreeLinkNode>();
var temp = root;
queue.Enqueue(temp);
while (queue.Count != 0)
{
temp = queue.Dequeue();
if (temp.left != null)
{
temp.left.next = temp.right;
queue.Enqueue(temp.left);
queue.Enqueue(temp.right);
if (temp.next != null) temp.right.next = temp.next.left;
}
}
}
//DFS recursive
public static void ConnectRecursive(TreeLinkNode root)
{
if (root == null) return;
if (root.left != null)
{
root.left.next = root.right;
if (root.next != null) root.right.next = root.next.left;
}
ConnectRecursive(root.left);
ConnectRecursive(root.right);
}
}
}