Skip to content

Instantly share code, notes, and snippets.

View HDegano's full-sized avatar

Herbert HDegano

  • Amazon.com
  • Austin, TX
View GitHub Profile
@HDegano
HDegano / SameTree.java
Created May 10, 2015 17:11
Check if two binary trees are equal
public class SameTree{
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null) return true;
if(p == null || q == null) return false;
if(p.val != q.val) return false;
@HDegano
HDegano / InOrderTraversal.java
Last active August 29, 2015 14:20
Inorder traverals of a binary tree
public class InOrderTraversal {
public List<Integer> inorderIterative(TreeNode root) {
List<Integer> inOrderList = new ArrayList<Integer>();
if(root == null) return inOrderList;
Stack<TreeNode> stack = new Stack<TreeNode>();
@HDegano
HDegano / IsBST.java
Last active August 29, 2015 14:20
Validate BST. Assumes no duplicate.
/*
Lets take advantage of a BST node property check that the left and right child
is within the range of its parent
*/
public class IsBST {
public boolean isValidBST(TreeNode root) {
if(root == null) return true;
return isValidBST(root, (long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE);
@HDegano
HDegano / BuySellStockNotes
Created May 8, 2015 02:42
Buy and Sell Stocks. One Transaction.
Questions
Can we return negative?
Initialize first entry as minimum
Set maxprice as 0 if we don't return negative
else get maxprofit right away and check for minimum before loop
Run through the array
for each entry, check if we have a new max profit, check if ar[i] is less than min
@HDegano
HDegano / BSTIterator.java
Last active August 29, 2015 14:20
Implement BSTIterator (Inorder)
/*
We need to build up on Inorder iterative
We can use a queue but that would take O(n) space
we can instead only push when we need to and only take up O(lgh), h is height
*/
public class BSTIterator {
Stack<TreeNode> stack;
public BSTIterator(TreeNode root) {
@HDegano
HDegano / ReverseSLL.cs
Last active August 29, 2015 14:20
Reverse a Link List
public class Solution{
public ListNode ReverseLinkList(ListNode head){
if(head == null) return null;
ListNode current = head;
ListNode prev = null;
ListNode reverseHead = null;
@HDegano
HDegano / FindKthNodeFromEnd.cs
Last active August 29, 2015 14:20
Find the Kth to The Last Node of a Singly Link List
public class Solution{
public ListNode FindKthNode(ListNode head, int n){
if(head == null || n <= 0) return null;
ListNode fast = head;
//Verify what is Kth to the last
// 1 -> 2 -> 3 and k = 1, if K = 3 or K = 2. For now assume the former is correct
@HDegano
HDegano / MinEditDistance.cs
Last active August 29, 2015 14:19
Given two strings, return the min edit distance
/*
Requires O(mn) space
*/
public class Solution {
public int MinDistance(string word1, string word2) {
int[,] dp = new int[word2.Length + 1, word1.Length + 1];
for (int i = 0; i <= word1.Length; i++) dp[0, i] = i;
for (int i = 0; i <= word2.Length; i++) dp[i, 0] = i;
@HDegano
HDegano / sortedArrayToBalancedBST.cs
Last active August 29, 2015 14:19
Sorted Array to Balanced BST
/**
* Definition for binary tree
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@HDegano
HDegano / MergeTwoList.cs
Last active August 29, 2015 14:19
Merge Two Sorted List
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {