This file contains hidden or 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
| /* | |
| * print a binary tree in level order using DFS & BFS | |
| */ | |
| package Chapter4; | |
| import java.util.Queue; | |
| import java.util.LinkedList; | |
| public class PrintLevelOrder { | |
| public static void main(String args[]){ |
This file contains hidden or 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
| /* | |
| * find lowest common ancestor in a Binary Tree | |
| */ | |
| package Chapter4; | |
| public class FindLCA_2 { | |
| public static void main(String args[]){ | |
| TreeNode n1 = new TreeNode(22, null, null); | |
| TreeNode n2 = new TreeNode(2, n1, null); | |
| TreeNode n4 = new TreeNode(4, null, null); |
This file contains hidden or 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
| /* | |
| * find lowest common ancestor in a Binary Search Tree(BST) | |
| */ | |
| package Chapter4; | |
| public class FindLCA_1 { | |
| public static void main(String args[]){ | |
| TreeNode n1 = new TreeNode(1, null, null); | |
| TreeNode n2 = new TreeNode(2, n1, null); | |
| TreeNode n4 = new TreeNode(4, null, null); |
This file contains hidden or 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 int uniquePaths(int m, int n) { | |
| int matrix[][] = new int[m + 1][n + 1]; | |
| return backtrack(matrix, 0 , 0, m, n); | |
| } | |
| public int backtrack(int[][] matrix, int c, int r, int m, int n){ | |
| if(c == m - 1 && r == n -1){ | |
| return 1; |
This file contains hidden or 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 int uniquePathsWithObstacles(int[][] obstacleGrid) { | |
| int m = obstacleGrid.length; | |
| int n = obstacleGrid[0].length; | |
| int matrix[][] = new int[m][n]; | |
| for(int i = n - 1; i >= 0; i--){ |
This file contains hidden or 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
| /* | |
| * build tree from inorder and preorder | |
| */ | |
| package Chapter4; | |
| public class BuildTree { | |
| public static void main(String args[]){ | |
| int pre[] = {7, 10, 4, 3,1,2,8,11}; | |
| int in[] = {4,10,3,1,7,11,8,2}; | |
| TreeNode root =buildTree(pre, in); |
This file contains hidden or 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
| import java.util.ArrayList; | |
| public class Solution { | |
| public ArrayList<ArrayList<Integer>> permute(int[] num) { | |
| return permuteRecursive(num, 0); | |
| } | |
| public ArrayList<ArrayList<Integer>> permuteRecursive(int[] num, int index){ | |
| if(index == num.length - 1){ | |
| ArrayList<Integer> list = new ArrayList<Integer>(); |
This file contains hidden or 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
| package Chapter2; | |
| /* | |
| * reverse a linked list | |
| */ | |
| public class ReverseList { | |
| public static void main(String args[]){ | |
| IntNode n1, n2, n3, n4, n5, n6,n7, n8, n9; | |
| n9 = new IntNode(10, null); | |
| n8 = new IntNode(9, n9); | |
| n7 = new IntNode(8, n8); |
This file contains hidden or 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
| package Chapter9; | |
| /* | |
| * 9-6 Given a matrix in which each row and each column is sorted, | |
| * write a method to find an element in it. | |
| */ | |
| public class Question9_6 { | |
| public static void main(String args[]){ | |
| int m[][]= { | |
| {1,4,5,6,8}, | |
| {2,5,7,8,10}, |
This file contains hidden or 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
| /* | |
| * Given a sorted array of strings which is interspersed with empty strings, | |
| * write a meth- od to find the location of a given string. | |
| Example: find “ball” in [“at”, “”, “”, “”, “ball”, “”, “”, “car”, “”, “”, “dad”, “”, “”] | |
| will return 4 | |
| Example: find “ballcar” in [“at”, “”, “”, “”, “”, “ball”, “car”, “”, “”, “dad”, “”, “”] | |
| will return -1 | |
| */ | |
| package Chapter9; |