-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseful snippets.cpp
More file actions
63 lines (58 loc) · 839 Bytes
/
Useful snippets.cpp
File metadata and controls
63 lines (58 loc) · 839 Bytes
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
lca(TreeNode* A, int val1, int val2)
{
deque<TreeNode>nodes;
TreeNode B(0);
nodes.push_back(*A);
if(B.left!=NULL)
{
nodes.push_back(*B.left);
parents[B.left->val]=B.val;
}
if(B.right!=NULL)
{
nodes.push_back(*B.right);
parents[B.right->val]=B.val;
}
}
class Node
{
Node next = null;
int data;
public Node(int d)
{
data = d;
}
void appendToTail(int d)
{
Node end = new Node(d);
Node n = this;
while (n.next != null)
{
n = n.next;
}
n.next = end;
}
}
int binarySearch(int[] a, int x)
{
int low = 0;
int high = a.length - 1;
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (a[mid] < x)
{
low = mid + 1;
}
else if (a[mid] > x)
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1; // Error
}