This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution{ | |
public ListNode ReverseLinkList(ListNode head){ | |
if(head == null) return null; | |
ListNode current = head; | |
ListNode prev = null; | |
ListNode reverseHead = null; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |