Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3fb39fc

Browse files
added p215
1 parent a9a4f3a commit 3fb39fc

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

‎AllQuestions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3333,7 +3333,7 @@ its binary representation.
33333333

33343334
For example, given 156, you should return 3.
33353335

3336-
## Problem-215:waxing_crescent_moon:
3336+
## [Problem-215](src/main/java/in/ashwanik/dcp/problems/p211_240/p215):sunny:
33373337

33383338

33393339
> This problem was asked by Yelp.

‎README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ Solutions to the coding problems from [Daily coding problem](https://dailycoding
107107
|[P66](src/main/java/in/ashwanik/dcp/problems/p61_90/p66)|[P103](src/main/java/in/ashwanik/dcp/problems/p91_120/p103)|
108108

109109

110+
## **Yelp (2)**
111+
| | |
112+
|--|--|
113+
|[P81](src/main/java/in/ashwanik/dcp/problems/p61_90/p81)|[P215](src/main/java/in/ashwanik/dcp/problems/p211_240/p215)|
114+
115+
110116
## **Jane Street (2)**
111117
| | |
112118
|--|--|
@@ -119,12 +125,6 @@ Solutions to the coding problems from [Daily coding problem](https://dailycoding
119125
|[P34](src/main/java/in/ashwanik/dcp/problems/p31_60/p34)|
120126

121127

122-
## **Yelp (1)**
123-
| |
124-
|--|
125-
|[P81](src/main/java/in/ashwanik/dcp/problems/p61_90/p81)|
126-
127-
128128
## **ContextLogic (1)**
129129
| |
130130
|--|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
The horizontal distance of a binary tree node describes how far left or right
3+
the node will be when the tree is printed out.
4+
5+
More rigorously, we can define it as follows:
6+
7+
* The horizontal distance of the root is 0.
8+
* The horizontal distance of a left child is hd(parent) - 1.
9+
* The horizontal distance of a right child is hd(parent) + 1.
10+
11+
For example, for the following tree, hd(1) = -2, and hd(6) = 0.
12+
```
13+
5
14+
/ \
15+
3 7
16+
/ \ / \
17+
1 4 6 9
18+
/ /
19+
0 8
20+
```
21+
22+
The bottom view of a tree, then, consists of the lowest node at each horizontal
23+
distance. If there are two nodes at the same depth and horizontal distance,
24+
either is acceptable.
25+
26+
For this tree, for example, the bottom view could be [0, 1, 3, 6, 8, 9].
27+
28+
Given the root to a binary tree, return its bottom view.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package in.ashwanik.dcp.problems.p211_p240.p215;
2+
3+
import in.ashwanik.dcp.common.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.TreeMap;
10+
11+
class Solution {
12+
13+
Collection<Integer> getBottomView(TreeNode<Integer> root) {
14+
List<Integer> values = new ArrayList<>();
15+
if (root == null) {
16+
return values;
17+
}
18+
Map<Integer, Integer> map = new TreeMap<>();
19+
populate(root, map, 0);
20+
return map.values();
21+
}
22+
23+
private void populate(TreeNode<Integer> root, Map<Integer, Integer> map, int distance) {
24+
if (root == null) {
25+
return;
26+
}
27+
28+
map.put(distance, root.getData());
29+
populate(root.getLeft(), map, distance - 1);
30+
populate(root.getRight(), map, distance + 1);
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package in.ashwanik.dcp.problems.p211_p240.p215;
2+
3+
import in.ashwanik.dcp.common.TreeNode;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
7+
8+
public class SolutionTest {
9+
@Test
10+
void testBottomView() {
11+
TreeNode<Integer> root = new TreeNode<>(5,
12+
new TreeNode<>(3,
13+
new TreeNode<>(1,
14+
new TreeNode<>(0),
15+
null),
16+
new TreeNode<>(4)),
17+
new TreeNode<>(7,
18+
new TreeNode<>(6),
19+
new TreeNode<>(9,
20+
new TreeNode<>(8),
21+
null)));
22+
23+
24+
assertArrayEquals(new Integer[]{0, 1, 3, 6, 8, 9}, new Solution().getBottomView(root).toArray());
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /