LeetCode – Median of Two Sorted Arrays (Java)

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Java Solution This problem can be converted to the problem of finding kth element, k is (A’s length + B’ Length)/2. If any of … Read more

LeetCode – Clone Graph (Java)

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. Java Solution 1 – DFS HashMap<Node, Node> map = new HashMap<>(); public Node cloneGraph(Node node) { map.put(node, new Node(node.val, new ArrayList<>()));   for(Node neighbor: node.neighbors){ if(map.containsKey(neighbor)){ map.get(node).neighbors.add(map.get(neighbor)); }else{ map.get(node).neighbors.add(cloneGraph(neighbor)); } }   return map.get(node); }HashMap<Node, Node> map … Read more

LeetCode – Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that only one letter can be changed at a time and each intermediate word must exist in the dictionary. For example, given: start = “hit” end = “cog” dict = [“hot”,”dot”,”dog”,”lot”,”log”] One shortest transformation … Read more

LeetCode – Reverse Integer

LeetCode – Reverse Integer: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 1. Naive Method We can convert the integer to a string/char array, reverse the order, and convert the string/char array back to an integer. However, this will require extra space for the string. It … Read more

Leetcode – Linked List Cycle

Given a linked list, determine if it has a cycle in it. Analysis If we have 2 pointers – fast and slow. It is guaranteed that the fast one will meet the slow one if there exists a circle. The problem can be demonstrated in the following diagram: Java Solution public class Solution { public … Read more

Leetcode – Word Break (Java)

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = “leetcode”, dict = [“leet”, “code”]. Return true because “leetcode” can be segmented as “leet code”. 1. Naive Approach This problem can be solve by … Read more

Leetcode – Same Tree

Two binary trees are considered the same if they have identical structure and nodes have the same value. This problem can be solved by using a simple recursive function. public boolean isSameTree(TreeNode p, TreeNode q) { if(p==null && q==null){ return true; }else if(p==null || q==null){ return false; }   if(p.val==q.val){ return isSameTree(p.left, q.left) && isSameTree(p.right, … Read more

Leetcode – Binary Tree Inorder Traversal (Java)

There are 3 solutions for solving this problem. Java Solution 1 – Iterative The key to solve inorder traversal of binary tree includes the following: The order of “inorder” is: left child -> parent -> right child Use a stack to track nodes public List<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> result = new ArrayList<>(); Stack<TreeNode> stack … Read more

Leetcode – Binary Tree Postorder Traversal (Java)

Among preoder, inorder and postorder binary tree traversal problems, postorder traversal is the most complicated one. For example, for the following tree, the post order traversal returns {4, 6, 5, 2, 3, 1}. Java Solution 1 The key to to iterative postorder traversal is the following: The order of “Postorder” is: left child -> right … Read more

LeetCode – Two Sum (Java)

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not … Read more