-
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
8fd702e
commit a299526
Showing
11 changed files
with
208 additions
and
20 deletions.
There are no files selected for viewing
Binary file not shown.
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package advance; | ||
package advance.trees; | ||
|
||
class AVLTree { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
src/advance/BinarySearchTree.java → src/advance/trees/BinarySearchTree.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package advance; | ||
package advance.trees; | ||
|
||
public class BinarySearchTree { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
src/advance/BinaryTree.java → src/advance/trees/BinaryTree.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package advance; | ||
package advance.trees; | ||
|
||
import java.util.Scanner; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
src/advance/BitManipulation.java → src/advance/trees/BitManipulation.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package advance; | ||
package advance.trees; | ||
|
||
public class BitManipulation { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
src/advance/BitwiseNums.java → src/advance/trees/BitwiseNums.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package advance; | ||
package advance.trees; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
|
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,105 @@ | ||
package advance.trees.interviews; | ||
|
||
import com.sun.source.tree.Tree; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
|
||
public class BFS { | ||
|
||
// https://leetcode.com/problems/binary-tree-level-order-traversal/ | ||
public List<List<Integer>> levelOrderTraversal(TreeNode root) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
if (root == null) | ||
return result; | ||
|
||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.add(root); | ||
|
||
while (!queue.isEmpty()) { | ||
int levelSize = queue.size(); | ||
|
||
// Get the current level list(the list at each level of the tree) | ||
List<Integer> subList = new ArrayList<>(levelSize); | ||
|
||
// Loop through the level | ||
for (int i = 0; i < levelSize; i++) { | ||
TreeNode currentNode = queue.poll(); | ||
if (currentNode != null) { | ||
subList.add(currentNode.value); | ||
|
||
if (currentNode.left != null) | ||
queue.offer(currentNode.left); | ||
if (currentNode.right != null) | ||
queue.offer(currentNode.right); | ||
} | ||
|
||
} | ||
result.add(subList); | ||
} | ||
return result; | ||
} | ||
|
||
// https://leetcode.com/problems/average-of-levels-in-binary-tree/description/ | ||
public List<Double> averageOfLevels(TreeNode root) { | ||
List<Double> result = new ArrayList<>(); | ||
|
||
if (root == null) | ||
return result; | ||
|
||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.offer(root); | ||
|
||
while (!queue.isEmpty()) { | ||
int levelSize = queue.size(); | ||
double averageOfEachLevel = 0; | ||
|
||
for (int i = 0; i < levelSize; i++) { | ||
TreeNode currentNode = queue.poll(); // the head or top node | ||
averageOfEachLevel += currentNode.value; | ||
|
||
if (currentNode.left != null) | ||
queue.offer(currentNode.left); | ||
if (currentNode.right != null) | ||
queue.offer(currentNode.right); | ||
} | ||
averageOfEachLevel /= levelSize; | ||
result.add(averageOfEachLevel); | ||
} | ||
return result; | ||
} | ||
|
||
/* | ||
Given the root of a binary tree, | ||
return the sum value of the nodes on each level in the form of an array. | ||
Answers within 10-5 of the actual answer will be accepted. | ||
*/ | ||
public List<Integer> sumOfLevels(TreeNode root) { | ||
List<Integer> result = new ArrayList<>(); | ||
|
||
if (root == null) | ||
return result; | ||
|
||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.offer(root); | ||
|
||
while (!queue.isEmpty()){ | ||
int levelSize=queue.size(); | ||
int sumOfAtLevel = 0; | ||
|
||
for (int i=0; i < levelSize; i++){ | ||
TreeNode currentNode = queue.poll(); // the topmost node | ||
sumOfAtLevel += currentNode.value; | ||
|
||
if (currentNode.left != null) | ||
queue.offer(currentNode.left); | ||
if (currentNode.right != null) | ||
queue.offer(currentNode.right); | ||
} | ||
result.add(sumOfAtLevel); | ||
} | ||
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,19 @@ | ||
package advance.trees.interviews; | ||
|
||
import com.sun.source.tree.Tree; | ||
|
||
class TreeNode { | ||
int value; | ||
TreeNode left; | ||
TreeNode right; | ||
|
||
TreeNode(int value, TreeNode left, TreeNode right){ | ||
this.value = value; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
TreeNode(int value){ | ||
this.value = value; | ||
} | ||
} |
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
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