-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter4.cs
More file actions
179 lines (146 loc) · 4.66 KB
/
Copy pathChapter4.cs
File metadata and controls
179 lines (146 loc) · 4.66 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
namespace CodingProblems
{
public class Chapter4
{
public static TreeNode Problem2(int[] sortedArray)
{
return Problem2(sortedArray, 0, sortedArray.Length - 1);
}
private static TreeNode Problem2(int[] sortedArray, int left, int right)
{
if (left > right)
{
return null;
}
int mid = (left + right) / 2;
TreeNode root = new TreeNode { Value = sortedArray[mid] };
root.Left = Problem2(sortedArray, left, mid - 1);
root.Right = Problem2(sortedArray, mid + 1, right);
return root;
}
private static int CheckHeight(TreeNode root)
{
if (root == null)
{
return -1;
}
int leftHeight = CheckHeight(root.Left);
if (leftHeight == int.MinValue) return int.MinValue;
int rightHeight = CheckHeight(root.Right);
if (rightHeight == int.MinValue) return int.MinValue;
int heightDiff = leftHeight - rightHeight;
if (Math.Abs(heightDiff) > 1)
{
return int.MinValue;
}
else
{
return Math.Max(leftHeight, rightHeight) + 1;
}
}
public static bool Problem4(TreeNode root)
{
return CheckHeight(root) != int.MinValue;
}
public bool Problem1(TreeNode root)
{
return IsBalanced(root);
}
public static int MaxDepth(TreeNode node)
{
if (node == null)
{
return 0;
}
return 1 + Math.Max(MaxDepth(node.Left), MaxDepth(node.Right));
}
public static int MinDepth(TreeNode node)
{
if (node == null)
{
return 0;
}
return 1 + Math.Min(MinDepth(node.Left), MinDepth(node.Right));
}
public static bool IsBalanced(TreeNode node)
{
return MaxDepth(node) - MinDepth(node) <= 1;
}
public static List<List<TreeNode>> InLevelProblem4(TreeNode root)
{
Queue<TreeNode> nextNodes = new Queue<TreeNode>();
nextNodes.Enqueue(root);
var result = new List<List<TreeNode>>();
while (nextNodes.Count > 0)
{
var levelList = new List<TreeNode>();
result.Add(levelList);
var nodesLevel = nextNodes.Count;
for (int i = 0; i < nodesLevel; i++)
{
var currentNode = nextNodes.Dequeue();
levelList.Add(currentNode);
if (currentNode.Left != null)
{
nextNodes.Enqueue(currentNode.Left);
}
if (currentNode.Right != null)
{
nextNodes.Enqueue(currentNode.Right);
}
}
}
return result;
}
public static List<int> InOrder(TreeNode root)
{
if (root == null)
{
return null;
}
var nodes = new List<int>();
InOrder(root, nodes);
return nodes;
}
public static void InOrder(TreeNode node, List<int> currentList)
{
if (node != null)
{
InOrder(node.Left, currentList);
currentList.Add(node.Value);
InOrder(node.Right, currentList);
}
}
public static List<int> InOrderIterative(TreeNode root)
{
var nodes = new List<int>();
var visited = new HashSet<TreeNode>();
var toVisit = new Stack<TreeNode>();
toVisit.Push(root);
while (toVisit.Count != 0)
{
var currentNode = toVisit.Pop();
if (!visited.Contains(currentNode))
{
visited.Add(currentNode);
if (currentNode.Right != null)
{
toVisit.Push(currentNode.Right);
}
toVisit.Push(currentNode);
if (currentNode.Left != null)
{
toVisit.Push(currentNode.Left);
}
}
else
{
nodes.Add(currentNode.Value);
}
}
return nodes;
}
}
}