-
Notifications
You must be signed in to change notification settings - Fork 483
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
Showing
5 changed files
with
183 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
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import java.util.*; | ||
|
||
public class CousinsInBinaryTreeII { | ||
|
||
public static void main(String[] args) { | ||
TreeNode treeNode = BinaryTree.createTreeNode(33, 37, 42, null, null, null, 46); | ||
TreeNode result = replaceValueInTree(treeNode); | ||
System.out.println("result = " + result); | ||
} | ||
|
||
public static TreeNode replaceValueInTree(TreeNode root) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
int level = 0; | ||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.add(root); | ||
while (!queue.isEmpty()) { | ||
int size = queue.size(); | ||
int sum = 0; | ||
while (size-- > 0) { | ||
TreeNode poll = queue.poll(); | ||
sum += poll.val; | ||
if (poll.left != null) queue.add(poll.left); | ||
if (poll.right != null) queue.add(poll.right); | ||
} | ||
map.put(level++, sum); | ||
} | ||
|
||
queue.add(root); | ||
level = 0; | ||
while (!queue.isEmpty()) { | ||
int size = queue.size(); | ||
while (size-- > 0) { | ||
TreeNode poll = queue.poll(); | ||
// At each node you set the nextLevelSum value of the child nodes, by reducing the nextLevelSum of values | ||
int nextLevelSum = 0; | ||
if (level <= 1) { | ||
poll.val = 0; | ||
} | ||
if (poll.left != null) { | ||
queue.add(poll.left); | ||
nextLevelSum += poll.left.val; | ||
} | ||
if (poll.right != null) { | ||
queue.add(poll.right); | ||
nextLevelSum += poll.right.val; | ||
} | ||
|
||
if (poll.left != null) poll.left.val = map.get(level + 1) - nextLevelSum; | ||
if (poll.right != null) poll.right.val = map.get(level + 1) - nextLevelSum; | ||
} | ||
level++; | ||
} | ||
|
||
return root; | ||
} | ||
|
||
} |
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
46 changes: 46 additions & 0 deletions
46
dataStructures/stack/MaximumNestingDepthOfTheParentheses.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,46 @@ | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
// https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/description/?envType=daily-question&envId=2024-04-04 | ||
public class MaximumNestingDepthOfTheParentheses { | ||
|
||
public static void main(String[] args) { | ||
System.out.println(maxDepth("(1+(2*3)+((8)/4))+1")); | ||
System.out.println(maxDepth("(1)+((2))+(((3)))")); | ||
} | ||
|
||
public static int maxDepth(String s) { | ||
int depth = 0; | ||
if (s.isEmpty()) return depth; | ||
Queue<String> queue = new LinkedList<>(); | ||
queue.add(s); | ||
while (!queue.isEmpty()) { | ||
int size = queue.size(); | ||
for (int i = 0; i < size; i++) { | ||
String poll = queue.poll(); | ||
int start = -1, cal = 0; | ||
for (int j = 0; j < poll.length(); j++) { | ||
char c = poll.charAt(j); | ||
if (c == '(') { | ||
cal++; | ||
if (start == -1) { | ||
start = j; | ||
} | ||
} else if (c == ')') { | ||
cal--; | ||
if (cal == 0) { | ||
queue.add(poll.substring(start + 1, j)); | ||
start = -1; | ||
} | ||
} | ||
} | ||
if (start != -1) { | ||
queue.add(poll.substring(start + 1)); | ||
} | ||
} | ||
depth++; | ||
// System.out.println("Depth " + depth + " Queue = " + queue); | ||
} | ||
return depth - 1; | ||
} | ||
} |