-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
97 lines (77 loc) · 2.12 KB
/
Program.cs
File metadata and controls
97 lines (77 loc) · 2.12 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
namespace LeetCodeProblems
{
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
}
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
public void DeleteNode(ListNode node)
{
if (node != null)
{
node.val = node.next.val;
node.next = node.next.next;
}
}
public static ListNode reverse(ListNode head)
{
ListNode prev = null;
while (head != null)
{
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
// bfs / dfs
static Node sample_tree()
{
Node newTree = new Node("A",
new Node("B",
new Node("C"), new Node("D")),
new Node("E",
new Node("F"), new Node("G",
new Node("H"), null)));
return newTree;
}
static void bfs_traversal(Node node)
{
Queue<Node> q = new Queue<Node>();
q.Enqueue(node);
while (q.Count > 0)
{
node = q.Dequeue();
Console.Write(node.data + " ");
if (node.left != null)
{
q.Enqueue(node.left);
}
if (node.right != null)
{
q.Enqueue(node.right);
}
}
}
static void dfs_traversal(Node node)
{
if (node == null)
{
return;
}
Console.Write(node.data + " ");
dfs_traversal(node.left);
dfs_traversal(node.right);
}
}
}