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<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){ |
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
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)); | |
} |
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 int minDepth(TreeNode A) { | |
if(A== null){ | |
return 0; | |
} | |
if(A.left == null && A.right == null){ | |
return 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 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; | |
} |
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<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); |
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<Integer> postorderTraversal(TreeNode A) { | |
Stack<TreeNode> s1 = new Stack<>(); | |
Stack<TreeNode> s2 = new Stack<>(); | |
if(A == null){ | |
return null; | |
} | |
s1.push(A); |
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<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); |
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<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; | |
} |
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 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); |