rsrs3
BAN USERclass TournamentTree {
/**
* This should return the second minimum
* int value in the given tournament tree
*/
public static Integer secondMin(Node root) {
if(root.left == null && root.right == null) {
return Integer.MAX_VALUE;
}
Node node;
int min;
if(root.left.value == root.value) {
node = root.left;
min = root.right.value;
} else {
node = root.right;
min = root.left.value;
}
return Math.min(min, secondMin(node));
}
}
public class MatrixZigZag {
public void print(int[][] matrix) {
for(int i = matrix.length - 1; i >=0; --i) {
printHelper(matrix, i, 0);
}
for(int j = 1; j < matrix[0].length; ++j) {
printHelper(matrix, 0, j);
}
}
private void printHelper(int[][] matrix, int row, int col) {
StringBuilder sb = new StringBuilder();
while(row < matrix.length && col < matrix[0].length) {
sb.append(matrix[row][col]).append(" ");
row = row + 1;
col = col + 1;
}
System.out.println(sb);
}
public static void main(String[] args) {
MatrixZigZag mz = new MatrixZigZag();
int[][] matrix1 = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int[][] matrix2 = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
mz.print(matrix1);
mz.print(matrix2);
}
}
public class NArrTreeTraversal {
private static class NArrTreeNode {
int val;
ArrayList<NArrTreeNode> children;
public NArrTreeNode(int value) {
val = value;
children = new ArrayList<>();
}
}
public ArrayList<Integer> postOrder(NArrTreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
HashSet<NArrTreeNode> hash = new HashSet<>();
Stack<NArrTreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
NArrTreeNode node = stack.peek();
if (node.children.size() == 0 || hash.contains(node)) {
result.add(stack.pop().val);
} else {
node.children.forEach(n -> stack.push(n));
hash.add(node);
}
}
return result;
}
}
public class NArrTreeTraversalModified {
private static class NArrTreeNodeModified {
int val;
ArrayList<NArrTreeNode> children;
boolean canVisit;
public NArrTreeNode(int value) {
val = value;
children = new ArrayList<>();
}
}
public ArrayList<Integer> postOrderTraversal(NArrTreeNodeModified root) {
ArrayList<Integer> result = new ArrayList<>();
Stack<NArrTreeNodeModified> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
NArrTreeNodeModified node = stack.peek();
if (node.children.size() == 0 || node.canVisit) result.add(stack.pop().val);
else {
node.children.forEach(n -> stack.push(n));
node.canVisit = true;
}
}
return result;
}
}
public class SpiralRotation {
public static void rotate(ArrayList<ArrayList<Integer>> matrix) {
int M = matrix.size();
int N = matrix.get(0).size();
int left = 0, right = N - 1, top = 0, bottom = M - 1;
int dir = 0, temp = Integer.MIN_VALUE;
while (left <= right && top <= bottom) {
if (dir == 0) {
if (left == right) break;
temp = matrix.get(top + 1).get(left);
for (int i = left; i <= right; ++i) {
int swap = matrix.get(top).get(i);
matrix.get(top).set(i, temp);
temp = swap;
}
top++;
}
if (dir == 1) {
for (int i = top; i <= bottom; ++i) {
int swap = matrix.get(i).get(right);
matrix.get(i).set(right, temp);
temp = swap;
}
right--;
}
if (dir == 2) {
for (int i = right; i >= left; --i) {
int swap = matrix.get(bottom).get(i);
matrix.get(bottom).set(i, temp);
temp = swap;
}
bottom--;
}
if (dir == 3) {
for (int i = bottom; i >= top; --i) {
int swap = matrix.get(i).get(left);
matrix.get(i).set(left, temp);
temp = swap;
}
left++;
}
dir = (dir + 1) % 4;
}
matrix.forEach(row -> System.out.println(row));
}
}
public class UDLinkedList {
private Node head;
public UDLinkedList(Node node) {
head = node;
}
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.down = new Node(6);
head.down.next = new Node(7);
head.down.next.next = new Node(10);
head.down.next.down = new Node(8);
head.down.next.down.next = new Node(9);
head.down.next.next.down = new Node(11);
head.down.next.next.down.next = new Node(12);
UDLinkedList list = new UDLinkedList(head);
UDLLIterator iterator = list.iterator();
try {
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public UDLLIterator iterator() {
return new UDLLIterator();
}
private static class Node {
private int value;
private Node next;
private Node down;
public Node(int v) {
this.value = v;
}
}
private class UDLLIterator {
private Stack<Node> stack;
public UDLLIterator() {
stack = new Stack<>();
stack.push(head);
}
public boolean hasNext() {
return !stack.isEmpty();
}
public int next() throws Exception {
if (hasNext()) {
Node current = stack.pop();
if (current.next != null) {
stack.push(current.next);
}
if (current.down != null) {
stack.push(current.down);
}
return current.value;
} else {
throw new Exception("No element to iterate over");
}
}
}
}
RepAveryPerez, abc at 8x8
I am a self-driven and motivated librarian skilled at providing excellent public service to students and staff, managing the library ...
RepI am a Website content writer who specialises in providing relevant content for websites. Every website has a specific target ...
- rsrs3 April 09, 2017