Skip to content

Instantly share code, notes, and snippets.

View kYroL01's full-sized avatar
🥊

Michele Campus kYroL01

🥊
View GitHub Profile
@kYroL01
kYroL01 / lowestCommAncBST.go
Last active July 25, 2021 11:59
Lowest Common Ancestor for a BST
/**
Algorithm
- Start traversing the tree from the root node.
- If both p and q are < root, then continue to left subtree and starting step 1.
- If both p and q are > root, then continue to right subtree and starting step 1.
- If both step 2 and step 3 are not true, we have found the LCA for p and q, and we return it
*/
/**
@kYroL01
kYroL01 / lowestCommAnc.go
Created July 25, 2021 12:08
Lowest Common Ancestor for a Binary Tree
/*
Algorithm
- if a node is NULL or equal p or q, we return it
- if not, we traverse the left and right subtree until we return a node, assigning to bool value RetLeft or RetRight
- Then we check if we have to return the node (RetLeft and RetRight are both != NULL), or only left or right
- The LCA is of course the node with RetLeft AND RetRight != NULL
*?
/**
* Definition for a binary tree node.
@kYroL01
kYroL01 / two_sum_fast.go
Created August 24, 2021 21:01
Fast two sum - alternative way instead of two for loops
func twoSum(nums []int, target int) []int {
var tempDict = make(map[int]int)
for key, value := range nums {
if _, ok := tempDict[value]; ok{
return []int{tempDict[value], key}
}
tempDict[target-value] = key
}
return []int{}
}
@kYroL01
kYroL01 / client.c
Created August 30, 2021 22:31
TCP client
/* -*- compile-command: "gcc -Wall -pedantic -g3 client.c -o client" -*- */
/**
TCP client implementation
1. Create TCP socket.
2. connect() Connect newly created client socket to server.
**/
#include <stdio.h>
@kYroL01
kYroL01 / server.c
Last active August 31, 2021 14:27
TCP server
/* -*- compile-command: "gcc -Wall -pedantic -g3 server.c -o server" -*- */
/**
TCP server implementation
1. Create a TCP socket.
2. bind(), Bind the socket to server address.
3. listen(), Put the server socket in a passive mode, it waits for the client to make a connection
4. accept(), Connection is established between client and server, ready to transfer data.
5. go back to Step 3.