Skip to content

Instantly share code, notes, and snippets.

View shharma-vipin's full-sized avatar
🏠
Working from home

Vipin Sharma shharma-vipin

🏠
Working from home
View GitHub Profile
@shharma-vipin
shharma-vipin / SliddingWindow.java
Created August 9, 2019 14:08
Slidding Window Problem- Find max number in each window of size K
public List<Integer> findMaxNumberInGivenSubArray(List<Intger> inputArray, int k){
Deque<Integer> deque = new LinkedList<>();
List<Integer> ouputArray = new ArrayList<>();
int i =0;
for(; i < k; i++){
while(!deque.isEmpty() && inputArray.get(deque.peekLast()) < inputArray.get(i)){
deque.removeLast();
}
dequeu.putLast(i);
@shharma-vipin
shharma-vipin / InorderIterative.java
Created August 9, 2019 14:51
Inorder Traversal without Recursion
public ArrayList<Integer> inorderTraversal(TreeNode A) {
Stack<TreeNode> stack = new Stack();
TreeNode current = A;
ArrayList<Integer> result = new ArrayList<Integer>();
while(current != null || !stack.isEmpty()){
while(current != null){
stack.push(current);
current = current.left;
}
@shharma-vipin
shharma-vipin / PreorderIterative.java
Created August 9, 2019 15:00
Preorder Traversal without Recursion
public ArrayList<Integer> preorderTraversal(TreeNode A) {
if(A == null) return null;
Stack<TreeNode> stack = new Stack<TreeNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
stack.push(A);
while(!stack.isEmpty()){
TreeNode temp = stack.pop();
result.add(temp.val);
@shharma-vipin
shharma-vipin / PostorderIterative.java
Created August 10, 2019 06:19
Postorder Travesal without Recursion
public ArrayList<Integer> postorderTraversal(TreeNode A) {
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
if(A == null){
return null;
}
s1.push(A);
@shharma-vipin
shharma-vipin / ZigZagTraversal.java
Created August 10, 2019 08:00
ZigZag Traversal Of Binary Tree
public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode A) {
Queue<TreeNode> q = new LinkedList<>();
if(A == null) return null;
Stack<TreeNode> currentLevel = new Stack<>();
Stack<TreeNode> nextLevel = new Stack<>();
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
currentLevel.push(A);
@shharma-vipin
shharma-vipin / MaxDepthTree.java
Created August 10, 2019 14:28
Maximum Depth Of a Tree
public int maxDepth(TreeNode A) {
if(A == null) return 0;
int left = maxDepth(A.left);
int right = maxDepth(A.right);
return Math.max(left,right)+1;
}
@shharma-vipin
shharma-vipin / MinimumDepthTree01.java
Created August 10, 2019 15:21
Minimum Depth Of Tree
public int minDepth(TreeNode A) {
if(A== null){
return 0;
}
if(A.left == null && A.right == null){
return 1;
}
@shharma-vipin
shharma-vipin / IdenticalTree.java
Created August 10, 2019 15:43
Check if two Trees are identical
private boolean checkIt(TreeNode A, TreeNode B){
if(A == null && B == null) return true;
if(A == null || B == null) return false;
return (A.val == B.val && checkIt(A.left,B.left) && checkIt(A.right,B.right));
}
@shharma-vipin
shharma-vipin / SumOfPathEqualK.java
Created August 11, 2019 12:31
Sum Of path equal to K in binary tree
public ArrayList<ArrayList<Integer>> pathSum(TreeNode A, int B) {
if(A == null) return null;
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
Deque<TreeNode> deque = new ArrayDeque();
doItNow(A,B,deque,res);
return res;
}
private void doItNow(TreeNode n, int m, Deque<TreeNode> deque,ArrayList<ArrayList<Integer>> res){