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

Java double Example

Have you ever met the situation that you get an integer but you really want a double? For the following method, devide(2,3) will return 0.0. public static double devide(int x, int y){ return x/y; }public static double devide(int x, int y){ return x/y; } The problem is that x/y does int division. If you want … Read more

Quicksort Array in Java

Quicksort is a divide and conquer algorithm. It first divides a large list into two smaller sub-lists and then recursively sort the two sub-lists. If we want to sort an array without any extra space, quicksort is a good option. On average, time complexity is O(n log(n)). The basic step of sorting an array are … Read more

Top 10 Algorithms for Coding Interview

This post summarizes the common subjects in coding interviews, including 1) String/Array/Matrix, 2) Linked List, 3) Tree, 4) Heap, 5) Graph, 6) Sorting, 7) Dynamic Programming, 8) Bit Manipulation, 9) Combinations and Permutations, and 10) Math. It is not alway easy to put a problem in one category, because the problem may belong to multiple … Read more

LeetCode – Sort List (Java)

LeetCode – Sort List: Sort a linked list in O(n log n) time using constant space complexity. Analysis If this problem does not have the constant space limitation, we can easily sort using a sorting method from Java SDK. With the constant space limitation, we need to do some pointer manipulation. Break the list to … Read more

Why Field Can’t Be Overridden?

This article shows the basic object oriented concept in Java – Field Hiding. 1. Can Field Be Overridden in Java? Let’s first take a look at the following example which creates two Sub objects. One is assigned to a Sub reference, the other is assigned to a Super reference. package oo;   class Super { … Read more

Loop an Iterable in Java

If you use some Java API and it returns an Iterable, such as Iterable, you may want to loop this iterable. The method is pretty simple as follows: Iterable<String> result = … //result returned from some method. for(String s: result){ System.out.println(s); }Iterable<String> result = … //result returned from some method. for(String s: result){ System.out.println(s); } … Read more

Iteration vs. Recursion in Java

1. Recursion Consider the factorial function: n!=n*(n-1)*(n-2)*…*1 There are many ways to compute factorials. One way is that n! is equal to n*(n-1)!. Therefore the program can be directly written as: Program 1: int factorial (int n) { if (n == 1) { return 1; } else { return n*factorial(n-1); } }int factorial (int n) … Read more

Loop Through a Given Directory With Indentation in Java

The code below provide a method for recursively loop through a directory in Java. It also prints out the directory hierarchy with indentation. package com.programcreek;   import java.io.File; import org.apache.commons.lang3.StringUtils;   public class LoopThroughADirectory {   public static void main(String[] args) { File[] files = new File("/home/programcreek/Desktop").listFiles(); showFiles(files); }   public static void showFiles(File[] files) … Read more