This file contains 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 ArrayList<ArrayList<TreeNode>> TreeToList(TreeNode root) { | |
ArrayList<ArrayList<TreeNode>> lst = new ArrayList<ArrayList<TreeNode>>(); | |
if (root == null) | |
return lst; | |
Queue<TreeNode> queue = new LinkedList<TreeNode>(); | |
queue.add(root); | |
queue.add(null); | |
ArrayList<TreeNode> level = new ArrayList<TreeNode>(); | |
while (!queue.isEmpty()) { | |
TreeNode node = queue.poll(); |
This file contains 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 TreeNode buildTree(int[] A) { | |
return buildBST(A, 0, A.length-1); | |
} | |
private TreeNode buildBST(int[] A, int left, int right) { | |
TreeNode root = null; | |
if (left <= right) { | |
int mid = left + (right - left) / 2; | |
root = new TreeNode(A[mid]); | |
root.left = buildBST(A, left, mid-1); |
This file contains 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 boolean isReachable(DirectedGraphNode p, DirectedGraphNode q) { | |
if (p == null || q == null) | |
return false; | |
if (p == q) | |
return true; | |
HashSet<DirectedGraphNode> visited = new HashSet<DirectedGraphNode>(); | |
Queue<DirectedGraphNode> queue = new LinkedList<DirectedGraphNode>(); | |
queue.add(p); | |
visited.add(p); | |
while (!queue.isEmpty()) { |
This file contains 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 boolean isBalanced(Tree root) { | |
if (checkHeight(root) == -1) | |
return false; | |
else | |
return true; | |
} | |
private int checkHeight(Tree root) { | |
if (root == null) | |
return 0; |
This file contains 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 AnimalSehelter { | |
private int id; | |
private Queue<Cat> Cats; | |
private Queue<Dog> Dogs; | |
public AnimalSehelter() { | |
this.id = 1; | |
this.Cats = new LinkedList<Cat>(); | |
this.Dogs = new LinkedList<Dog>(); | |
} |
This file contains 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 static void stackSort(Stack<Integer> stack) { | |
Stack<Integer> temp = new Stack<Integer>(); | |
while(!stack.isEmpty()) { | |
int val = stack.pop(); | |
while(!temp.isEmpty() && val > temp.peek()) { | |
stack.push(temp.pop()); | |
} | |
temp.push(val); | |
} | |
while(!temp.isEmpty()) { |
This file contains 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 MyQueue{ | |
private Stack<Integer> pushStack, popStack; | |
public MyQueue() { | |
pushStack = new Stack<Integer>(); | |
popStack = new Stack<Integer>(); | |
} | |
public void enqueue(int val) { |
This file contains 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 HanoiTower { | |
private int nDisks; | |
private ArrayList<Stack<Integer>> lstTowers; | |
public HanoiTower(int n) { | |
this.nDisks = n; | |
this.lstTowers = new ArrayList<Stack<Integer>>(); | |
lstTowers.add(new Stack<Integer>()); | |
lstTowers.add(new Stack<Integer>()); |
This file contains 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 SetOfStack{ | |
private ArrayList<Stack<Integer>> lstStack; | |
private int capacity; | |
public SetOfStack(int n) { | |
this.capacity = n; | |
this.lstStack = new ArrayList<Stack<Integer>>(); | |
} | |
This file contains 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 StackMin{ | |
private int[] Stk; | |
private int top_s; | |
private int[] Min; | |
private int top_m; | |
public StackMin() { | |
this.Stk = new int[1]; | |
this.Min = new int[1]; | |
this.top_s = -1; |