## LeetCode – Minimum Height Trees (Java)

For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of … Read more

## LeetCode – Largest BST Subtree (Java)

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Java Solution class Wrapper{ int size; int lower, upper; boolean isBST;   public Wrapper(){ lower = Integer.MAX_VALUE; upper = Integer.MIN_VALUE; isBST = false; size = 0; } } public … Read more

## LeetCode – Combination Sum IV (Java)

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Java Solution This problem is similar to Coin Change. It’s a typical dynamic programming problem. public int combinationSum4(int[] nums, int target) { if(nums==null || nums.length==0) return 0;   int[] dp … Read more

## LeetCode – Wiggle Subsequence (Java)

Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order. Java Solution The problem is converted to finding the turning point. When nums[i] … Read more

## LeetCode – Factor Combinations (Java)

Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a function that takes an integer n and return all possible combinations of its factors. Note: You may assume that n is always positive. Factors should be greater than 1 and less … Read more

## LeetCode – Find Leaves of Binary Tree (Java)

Given a binary tree, collect a tree’s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Example: Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Java Solution 1 Naively, we can get the order of each … Read more

## LeetCode – Word Pattern II (Java)

This is the extension problem of Word Pattern I. Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str. Examples: pattern = “abab”, str = “redblueredblue” should return … Read more

## LeetCode – Product of Array Except Self (Java)

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Java Solution 1 public int[] productExceptSelf(int[] nums) { int[] result = … Read more

## LeetCode – Delete Node in a Linked List (Java)

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling … Read more

## Implement a Queue using an Array in Java

There following Java code shows how to implement a queue without using any extra data structures in Java. We can implement a queue by using an array. import java.lang.reflect.Array; import java.util.Arrays;   public class Queue<E> {   E[] arr; int head = -1; int tail = -1; int size;   public Queue(Class<E> c, int size) … Read more

## LeetCode – Lowest Common Ancestor of a Binary Tree (Java)

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Java Solution 1 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root==null) return null;   if(root==p || root==q) return root;   TreeNode l = lowestCommonAncestor(root.left, p, q); TreeNode r = lowestCommonAncestor(root.right, p, q);   if(l!=null&&r!=null){ return root; … Read more

## LeetCode – Lowest Common Ancestor of a Binary Search Tree (Java)

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. Analysis This problem can be solved by using BST property, i.e., left < parent < right for each node. There are 3 cases to handle. Java Solution 1 – Recursive public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, … Read more

## LeetCode – Best Meeting Point (Java)

A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x – p1.x| + |p2.y – … Read more

## LeetCode – Guess Number Higher or Lower (Java)

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I’ll tell you whether the number is higher or lower. You call a pre-defined API guess(int num) which returns 3 possible results (-1, … Read more

## LeetCode – Shortest Word Distance II (Java)

This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which receives a list of words in the constructor, and implements a method that … Read more