Professional Documents
Culture Documents
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
this.val = val;
this.left = left;
this.right = right;
class Solution {
TreeNode first;
TreeNode prev;
TreeNode middle;
TreeNode last;
if(node == null){
return;
inorder(node.left);
if( prev != null && (node.val < prev.val)){
if(first == null){
first = prev;
middle = node;
else{
last = node;
prev = node;
inorder(node.right);
inorder(root);
first.val = last.val;
last.val = temp;
first.val = middle.val;
middle.val = temp;
}
return null;
return root;
if (root == null) {
return;
inorder(root.left);
inorder(root.right);
if (!nodesStr[i].equals("N")) {
nodes[i] = Integer.parseInt(nodesStr[i]);
} else {
nodes[i] = null;
solution.recoverTree(root);
inorder(root);
scanner.close();
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x) {
val = x;
}
public class Main {
if (root == null)
return result;
queue.add(root);
while (!queue.isEmpty()) {
if (i == size - 1) {
result.add(node.val);
if (node.left != null) {
queue.offer(node.left);
if (node.right != null) {
queue.offer(node.right);
return result;
return null;
queue.offer(root);
for (int i = 1; i < tokens.length; i += 2) {
if (!tokens[i].equals("-1")) {
queue.offer(parent.left);
queue.offer(parent.right);
return root;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
TreeNode node;
int hd;
this.node = node;
this.hd = hd;
while (!queue.isEmpty()) {
int hd = qNode.hd;
if (!levelMap.containsKey(hd)) {
levelMap.get(hd).add(node.val);
if (node.left != null) {
if (node.right != null) {
int hd = entry.getKey();
if (!map.containsKey(hd)) {
map.get(hd).addAll(nodes);
result.addAll(list);
}
return result;
queue.add(root);
if (!values[i].equals("N")) {
parent.left = leftChild;
queue.add(leftChild);
parent.right = rightChild;
queue.add(rightChild);
return root;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
result.add(root.val);
addLeftBoundary(root.left, result);
addLeaves(root, result);
addRightBoundary(root.right, result);
return result;
result.add(node.val);
if (node.left != null) {
addLeftBoundary(node.left, result);
} else {
addLeftBoundary(node.right, result);
if (node.right != null) {
addRightBoundary(node.right, result);
} else {
addRightBoundary(node.left, result);
result.add(node.val);
result.add(node.val);
return;
}
addLeaves(node.left, result);
addLeaves(node.right, result);
queue.add(root);
int i = 1;
if (!values[i].equals("-1")) {
parent.left = leftChild;
queue.add(leftChild);
i++;
parent.right = rightChild;
queue.add(rightChild);
i++;
return root;
}
public static void main(String[] args) {
//Graph - BFS
import java.util.*;
private int V;
BFSGraphTraversal(int v) {
V = v;
adj[v].add(w);
void BFS(int s) {
boolean visited[] = new boolean[V];
visited[s] = true;
queue.add(s);
while (queue.size() != 0) {
s = queue.poll();
System.out.print(s+" ");
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n]) {
visited[n] = true;
queue.add(n);
if (nodes == 0) {
return;
while (true) {
src = scanner.nextInt();
dest = scanner.nextInt();
if (src == -1 && dest == -1)
break;
g.addEdge(src, dest);
System.out.print("BFS : ");
g.BFS(0);
//DFS
import java.util.*;
int V;
LinkedList<Integer> adj[];
DFS(int v) {
V = v;
adj[v].add(e);
visited[s] = true;
dFSrec(n, visited);
void dFS(int s) {
dFSrec(s, visited);
int n = s.nextInt();
int v, e;
while (true) {
v = s.nextInt();
e = s.nextInt();
if (v == -1 && e == -1)
break;
graph.addEdges(v, e);
int S = s.nextInt();
System.out.print("DFS: ");
graph.dFS(S);
//Dials algorithm
import java.util.*;
public class Graph {
static final int INF = Integer.MAX_VALUE;
private int V;
private ArrayList<ArrayList<Tuple>> adj;
public Graph(int v) {
this.V = v;
this.adj = new ArrayList<ArrayList<Tuple>>();
for (int i = 0; i < v; i++)
this.adj.add(new ArrayList<Tuple>());
}
Tuple(int v, int w) {
this.v = v;
this.w = w;
}
}
this.source = source;
this.destination = destination;
this.weight = weight;
int n = scanner.nextInt();
int m = scanner.nextInt();
int v = scanner.nextInt();
int w = scanner.nextInt();
int src = 0;
Arrays.fill(distances, Integer.MAX_VALUE);
distances[src] = 0;
int u = edge.source;
int v = edge.destination;
int u = edge.source;
int v = edge.destination;
int weight = edge.weight;
Arrays.fill(distances, -1);
break;
return distances;
//Topological sort
import java.util.*;
int V;
LinkedList<Integer> adj[];
Main(int v) {
V = v;
adj[v].add(e);
visited[v] = true;
Integer i;
Iterator<Integer> it = adj[v].iterator();
while (it.hasNext()) {
i = it.next();
if (!visited[i])
stack.push(new Integer(v));
void topologicalSort() {
visited[i] = false;
if (!visited[i])
while (!stack.empty())
int n = s.nextInt();
int v, e;
while (true) {
v = s.nextInt();
e = s.nextInt();
if (v == -1 && e == -1)
break;
graph.addEdge(v, e);
graph.topologicalSort();
int n = scanner.nextInt();
arr[i] = scanner.nextInt();
heapSort(arr);
int n = arr.length;
heapify(arr, n, i);
}
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i) {
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
queue.add(root);
while (!queue.isEmpty()) {
rightmost = node;
result.add(rightmost.val);
return result;
if (nodes[i] != null) {
if (treeNodes[i] != null) {
int leftIndex = 2 * i + 1;
int rightIndex = 2 * i + 2;
treeNodes[i].left = treeNodes[leftIndex];
treeNodes[i].right = treeNodes[rightIndex];
return root;
if (!values[i].equals("null")) {
nodes[i] = Integer.parseInt(values[i]);
}
System.out.print(result.get(i)+" ");
//Winner Tree
import java.io.*;
import java.util.*;
String input=sc.nextLine();
for(int i=0;i<arr.length;i++){
arr1.add(Integer.parseInt(arr[i]));
Collections.sort(arr1);
System.out.println(arr1.get(1));
//all views(functions)
import java.util.*;
class Node {
int data;
Node(int data) {
this.data = data;
// Right View
if (root == null)
return;
q.add(root);
while (!q.isEmpty()) {
int n = q.size();
if (i == n - 1) {
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
// Left View
if (root == null)
return;
queue.add(root);
while (!queue.isEmpty()) {
int n = queue.size();
if (i == 1)
if (temp.left != null)
queue.add(temp.left);
if (temp.right != null)
queue.add(temp.right);
// Top View
if (root == null)
return;
int min = 0;
int max = 0;
while (!q.isEmpty()) {
if (!map.containsKey(curr.hd))
map.put(curr.hd, curr.node.data);
if (curr.node.left != null) {
if (curr.node.right != null) {
// Bottom View
if (root == null)
return;
root.hd = 0;
queue.add(root);
while (!queue.isEmpty()) {
int hd = temp.hd;
map.put(hd, temp.data);
if (temp.left != null) {
temp.left.hd = hd - 1;
queue.add(temp.left);
if (temp.right != null) {
temp.right.hd = hd + 1;
queue.add(temp.right);
}
for (int value : map.values())
Node node;
int hd;
this.node = node;
this.hd = hd;
Node root = new Node(1); // Change this with appropriate tree construction
System.out.println("Right View:");
ob.rightView(root);
System.out.println("\nLeft View:");
ob.leftView(root);
System.out.println("\nTop View:");
ob.topView(root);
System.out.println("\nBottom View:");
ob.bottomView(root);
//K-array
import java.util.*;
private int k;
this.k = k;
this.capacity = capacity;
this.size = 0;
return (i - 1) / k;
return k * i + j;
if (size == capacity) {
System.out.println("Heap overflow");
return;
size++;
int i = size - 1;
heap[i] = key;
heap[i] = heap[parent(i)];
heap[parent(i)] = temp;
i = parent(i);
int max = i;
max = c;
if (max != i) {
heap[i] = heap[max];
heap[max] = temp;
maxHeapify(max);
if (size <= 0)
return Integer.MIN_VALUE;
if (size == 1) {
size--;
return heap[0];
size--;
maxHeapify(0);
return root;
}
return;
size = arr.length;
heap[i] = arr[i];
maxHeapify(i);
list.add(heap[i]);
return list;
arr[i] = scanner.nextInt();
heap.buildHeap(arr);
System.out.println();
heapList.add(3);
System.out.println();
scanner.close();