-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9f715b
commit a2a70ea
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
public class Solution { | ||
public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode A) { | ||
ArrayList<ArrayList<Integer>> result = new ArrayList<>(); | ||
if (A == null) { | ||
return result; | ||
} | ||
|
||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.add(A); | ||
boolean leftToRight = true; | ||
|
||
while (!queue.isEmpty()) { | ||
int size = queue.size(); | ||
ArrayList<Integer> level = new ArrayList<>(size); | ||
|
||
for (int i = 0; i < size; i++) { | ||
TreeNode node = queue.poll(); | ||
|
||
if (leftToRight) { | ||
level.add(node.val); | ||
} else { | ||
level.add(0, node.val); | ||
} | ||
|
||
if (node.left != null) { | ||
queue.add(node.left); | ||
} | ||
if (node.right != null) { | ||
queue.add(node.right); | ||
} | ||
} | ||
|
||
leftToRight = !leftToRight; | ||
|
||
result.add(level); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Definition for binary tree | ||
* class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode(int x) { | ||
* val = x; | ||
* left=null; | ||
* right=null; | ||
* } | ||
* } | ||
*/ | ||
public class Solution { | ||
public TreeNode solve(TreeNode A, int B) { | ||
return deleteNode(A, B); | ||
} | ||
|
||
private TreeNode deleteNode(TreeNode root, int key){ | ||
if(root == null){ | ||
return null; | ||
} | ||
|
||
if(key < root.val){ | ||
root.left = deleteNode(root.left, key); | ||
}else if(key > root.val){ | ||
root.right = deleteNode(root.right, key); | ||
} | ||
else{ | ||
if(root.left == null && root.right == null){ | ||
return null; | ||
} | ||
else if(root.left == null || root.right == null){ | ||
return (root.left != null) ? root.left : root.right; | ||
}else{ | ||
TreeNode predecessor = findMax(root.left); | ||
root.val = predecessor.val; | ||
root.left = deleteNode(root.left, predecessor.val); | ||
} | ||
} | ||
return root; | ||
} | ||
|
||
private TreeNode findMax(TreeNode node){ | ||
while(node.right != null){ | ||
node = node.right; | ||
} | ||
return node; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Classes/module 6/9 - trees iii/Sorted Arr to bst/Solution.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
public class Solution { | ||
public TreeNode sortedArrayToBST(final int[] A) { | ||
if (A == null || A.length == 0) { | ||
return null; | ||
} | ||
return sortedArrayToBST(A, 0, A.length - 1); | ||
} | ||
|
||
private TreeNode sortedArrayToBST(final int[] A, int start, int end) { | ||
if (start > end) { | ||
return null; | ||
} | ||
|
||
int mid = start + (end - start) / 2; | ||
TreeNode node = new TreeNode(A[mid]); | ||
node.left = sortedArrayToBST(A, start, mid - 1); | ||
node.right = sortedArrayToBST(A, mid + 1, end); | ||
|
||
return node; | ||
} | ||
|
||
public static void printInOrder(TreeNode root) { | ||
if (root != null) { | ||
printInOrder(root.left); | ||
System.out.print(root.val + " "); | ||
printInOrder(root.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
public class Solution { | ||
public int isValidBST(TreeNode A) { | ||
return isValidBST(A, null, null) ? 1 : 0; | ||
} | ||
|
||
private boolean isValidBST(TreeNode node, Integer lower, Integer upper) { | ||
if (node == null) { | ||
return true; | ||
} | ||
|
||
int val = node.val; | ||
|
||
if (lower != null && val <= lower) { | ||
return false; | ||
} | ||
|
||
if (upper != null && val >= upper) { | ||
return false; | ||
} | ||
|
||
if (!isValidBST(node.right, val, upper)) { | ||
return false; | ||
} | ||
|
||
if (!isValidBST(node.left, lower, val)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Classes/module 7/2 - heaps ii/Ath largest element/Solution.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.util.PriorityQueue; | ||
|
||
public class Solution { | ||
public int[] solve(int A, int[] B) { | ||
int N = B.length; | ||
int[] result = new int[N]; | ||
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); | ||
|
||
for (int i = 0; i < N; i++) { | ||
minHeap.offer(B[i]); | ||
|
||
if (minHeap.size() < A) { | ||
result[i] = -1; | ||
} else { | ||
if (minHeap.size() > A) { | ||
minHeap.poll(); | ||
} | ||
result[i] = minHeap.peek(); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |