Skip to content

Instantly share code, notes, and snippets.

public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1==null) return t2;
if(t2==null) return t1;
//use st1 for t1 and st2 for t2. You can use one stack as well where you store pairs
Stack<TreeNode> st1 = new Stack<>();
Stack<TreeNode> st2 = new Stack<>();
//invariant: neither n1 or n2 can be null
st1.push(t1);
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
//base case
if(t1==null&&t2==null) return null;
if(t1==null) return t2;
if(t2==null) return t1;
//divide and conquer
TreeNode root = new TreeNode(t1.val+t2.val);
root.left = mergeTrees(t1.left, t2.left);
root.right = mergeTrees(t1.right, t2.right);
import java.util.*;
public class TopologicalSort {
public static void main(String args[]) {
smallGraphExample();
biggerGraphExample();
}
private static void biggerGraphExample() {
Set<Integer> vertices = new HashSet<>();
/**
The code below addresses these 2 problems:
Search a 2D Matrix - LeetCode: https://leetcode.com/problems/search-a-2d-matrix/
> Every row is sorted left to right in increasing order. The first integer of
each row is greater than the last integer of the previous row.
Search a 2D Matrix II - LeetCode: https://leetcode.com/problems/search-a-2d-matrix-ii/
> Every row is sorted left to right in increasing order
> Every column is sorted top to bottom in increasing order
/**
Minimum Window Substring - LeetCode: https://leetcode.com/problems/minimum-window-substring/
@author Benyam Ephrem (and I referenced the Leetcode solution for the optimal)
In the optimal solution I nest if statements and have redundant boolean values...I
know. This code is for learning purposes. My goal is for someone who reads this to
understand very quickly so that style can be adjusted to the programmer's desires.
The video to explain this code is here: https://www.youtube.com/watch?v=eS6PZLjoaq8
/*
Implement Queue using Stacks - LeetCode: https://leetcode.com/problems/implement-queue-using-stacks/
Authorship: Credit for the code in this file goes to the authors of the
book "Elements of Programming Interviews" by Adnan Aziz, Amit Prakash, and
Tsung-Hsien Lee.
This code passes all Leetcode test cases as of Feb. 4 2019
Runtime: 54 ms, faster than 95.59% of Java online submissions for Implement Queue using Stacks.
Memory Usage: 25.7 MB, less than 81.58% of Java online submissions for Implement Queue using Stacks.
/*
Implement Queue using Stacks - LeetCode: https://leetcode.com/problems/implement-queue-using-stacks/
Authorship: Credit for the code in this file goes to the authors of the
book "Elements of Programming Interviews" by Adnan Aziz, Amit Prakash, and
Tsung-Hsien Lee.
This code passes all Leetcode test cases as of Feb. 4 2019
Runtime: 54 ms, faster than 95.59% of Java online submissions for Implement Queue using Stacks.
Memory Usage: 25.7 MB, less than 81.58% of Java online submissions for Implement Queue using Stacks.
/**
@author Benyam Ephrem
Sort A Nearly Sorted Array: https://www.geeksforgeeks.org/nearly-sorted-algorithm/
I haven't written automated tests for this code so no guarantees. But it has been
manually tested thoroughly.
The video to explain this code: https://www.youtube.com/watch?v=yQ84lk-EXTQ
*/
/**
@author Benyam Ephrem
Sort A Nearly Sorted Array: https://www.geeksforgeeks.org/nearly-sorted-algorithm/
I haven't written automated tests for this code so no guarantees. But it has been
manually tested thoroughly.
The video to explain this code: https://www.youtube.com/watch?v=yQ84lk-EXTQ
*/
/*
A min heap implementation
Array Form: [ 5, 7, 6, 10, 15, 17, 12 ]
Complete Binary Tree Form:
5
/ \
7 6
/ \ / \