LeetCode – Implement Trie (Prefix Tree) (Java)

Implement a trie with insert, search, and startsWith methods. Java Solution 1 A trie node should contains the character, its children and the flag that marks if it is a leaf node. You can use the trie in the following diagram to walk though the Java solution. class TrieNode { char c; HashMap<Character, TrieNode> children … Read more

LeetCode – Shortest Distance from All Buildings (Java)

Java Solution This problem can be solved by BFS. We define one matrix for tracking the distance from each building, and another matrix for tracking the number of buildings which can be reached. public int shortestDistance(int[][] grid) { int[][] distance = new int[grid.length][grid[0].length]; int[][] reach = new int[grid.length][grid[0].length];     int numBuilding = 0; for … Read more

LeetCode – Recover Binary Search Tree (Java)

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Java Solution Inorder traveral will return values in an increasing order. So if an element is less than its previous element,the previous element is a swapped node. public class Solution { TreeNode first; TreeNode second; TreeNode … Read more

LeetCode – Maximum Depth of Binary Tree (Java)

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Java Solution public int maxDepth(TreeNode root) { if(root==null) return 0;   int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right);   int bigger = Math.max(leftDepth, rightDepth); … Read more

LeetCode – Binary Tree Paths (Java)

Given a binary tree, return all root-to-leaf paths. 1. Naive DFS Solution A typical depth-first search problem. public List<String> binaryTreePaths(TreeNode root) { ArrayList<String> finalResult = new ArrayList<String>();   if(root==null) return finalResult;   ArrayList<String> curr = new ArrayList<String>(); ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();   dfs(root, results, curr);   for(ArrayList<String> al : results){ StringBuilder sb = new … Read more

LeetCode – Closest Binary Search Tree Value (Java)

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Java Solution 1 – Recursion Recursively traverse down the root. When target is less than root, go left; when target is greater than root, go right. public class Solution { int goal; double … Read more

LeetCode – Flatten Nested List Iterator (Java)

Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a list — whose elements may also be integers or other lists. For example, given the list [[1,1],2,[1,1]], by calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]. … Read more

LeetCode – Top K Frequent Elements (Java)

Given an array of integers, write a method to return the k most frequent elements. Java Solution 1 – Heap Time complexity is O(n*log(k)). Note that heap is often used to reduce time complexity from n*log(n) (see solution 3) to n*log(k). public List<Integer> topKFrequent(int[] nums, int k) { //count the frequency for each element HashMap<Integer, … Read more

LeetCode – Group Shifted Strings (Java)

Given a string, we can “shift” each of its letter to its successive letter, for example: “abc” -> “bcd”. We can keep “shifting” which forms the sequence: “abc” -> “bcd” -> … -> “xyz”. Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence, return: … Read more