Skip to content

Commit 4e6596d

Browse files
p117
1 parent 994647e commit 4e6596d

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

AllQuestions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,7 @@ Generate a finite, but an arbitrarily large binary tree quickly in O(1).
18631863

18641864
That is, generate() should return a tree whose size is unbounded but finite.
18651865

1866-
## Problem-117:waxing_crescent_moon:
1866+
## [Problem-117](src/main/java/in/ashwanik/dcp/problems/p91_120/p117):sunny:
18671867

18681868

18691869
> This problem was asked by Facebook.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Solutions to the coding problems from [Daily coding problem](https://dailycoding
1111
|[P94](src/main/java/in/ashwanik/dcp/problems/p91_120/p94)|[P100](src/main/java/in/ashwanik/dcp/problems/p91_120/p100)|[P104](src/main/java/in/ashwanik/dcp/problems/p91_120/p104)|[P108](src/main/java/in/ashwanik/dcp/problems/p91_120/p108)|[P111](src/main/java/in/ashwanik/dcp/problems/p91_120/p111)|[P113](src/main/java/in/ashwanik/dcp/problems/p91_120/p113)|[P115](src/main/java/in/ashwanik/dcp/problems/p91_120/p115)|
1212

1313

14-
## **Facebook (16)**
14+
## **Facebook (17)**
1515
| | | | | | | | | | | | | | | |
1616
|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|
1717
|[P7](src/main/java/in/ashwanik/dcp/problems/p1_30/p7)|[P15](src/main/java/in/ashwanik/dcp/problems/p1_30/p15)|[P19](src/main/java/in/ashwanik/dcp/problems/p1_30/p19)|[P25](src/main/java/in/ashwanik/dcp/problems/p1_30/p25)|[P27](src/main/java/in/ashwanik/dcp/problems/p1_30/p27)|[P30](src/main/java/in/ashwanik/dcp/problems/p1_30/p30)|[P41](src/main/java/in/ashwanik/dcp/problems/p31_60/p41)|[P47](src/main/java/in/ashwanik/dcp/problems/p31_60/p47)|[P51](src/main/java/in/ashwanik/dcp/problems/p31_60/p51)|[P60](src/main/java/in/ashwanik/dcp/problems/p31_60/p60)|[P62](src/main/java/in/ashwanik/dcp/problems/p61_90/p62)|[P69](src/main/java/in/ashwanik/dcp/problems/p61_90/p69)|[P79](src/main/java/in/ashwanik/dcp/problems/p61_90/p79)|[P85](src/main/java/in/ashwanik/dcp/problems/p61_90/p85)|[P110](src/main/java/in/ashwanik/dcp/problems/p91_120/p110)|
18-
|[P114](src/main/java/in/ashwanik/dcp/problems/p91_120/p114)|
18+
|[P114](src/main/java/in/ashwanik/dcp/problems/p91_120/p114)|[P117](src/main/java/in/ashwanik/dcp/problems/p91_120/p117)|
1919

2020

2121
## **Microsoft (11)**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Given a binary tree, return the level of the tree with minimum sum.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package in.ashwanik.dcp.problems.p91_120.p117;
2+
3+
import in.ashwanik.dcp.common.TreeNode;
4+
5+
import java.util.ArrayDeque;
6+
import java.util.Deque;
7+
8+
class Solution {
9+
10+
int getLevelWithMinimumSum(TreeNode<Integer> root) {
11+
if (root == null) {
12+
return -1;
13+
}
14+
15+
Deque<TreeNode<Integer>> queue = new ArrayDeque<>();
16+
int minSum = Integer.MAX_VALUE;
17+
int minLevel = -1;
18+
int currentSum = 0;
19+
int level = 0;
20+
queue.offer(root);
21+
TreeNode<Integer> dummy = new TreeNode<>(0);
22+
23+
queue.offer(dummy);
24+
25+
while (!queue.isEmpty()) {
26+
TreeNode<Integer> current = queue.removeFirst();
27+
if (current == dummy) {
28+
if (currentSum < minSum) {
29+
minSum = currentSum;
30+
minLevel = level;
31+
}
32+
if (!queue.isEmpty()) {
33+
queue.offer(dummy);
34+
}
35+
level++;
36+
currentSum = 0;
37+
} else {
38+
currentSum += current.getData();
39+
if (current.getLeft() != null) {
40+
queue.offer(current.getLeft());
41+
}
42+
if (current.getRight() != null) {
43+
queue.offer(current.getRight());
44+
}
45+
}
46+
}
47+
return minLevel;
48+
}
49+
50+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package in.ashwanik.dcp.problems.p91_120.p117;
2+
3+
import in.ashwanik.dcp.common.TreeNode;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class SolutionTest {
9+
@Test
10+
void testMinimumSumLevel() {
11+
TreeNode<Integer> root = new TreeNode<>(2,
12+
new TreeNode<>(3,
13+
new TreeNode<>(-5),
14+
new TreeNode<>(4)),
15+
new TreeNode<>(-2));
16+
17+
assertEquals(2, new Solution().getLevelWithMinimumSum(root));
18+
}
19+
}

0 commit comments

Comments
 (0)