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 7a7c5d6

Browse files
p204
1 parent 1dfc9e9 commit 7a7c5d6

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
@@ -3213,7 +3213,7 @@ array does not contain duplicates.
32133213

32143214
For example, given [5, 7, 10, 3, 4], return 3.
32153215

3216-
## Problem-204:waxing_crescent_moon:
3216+
## [Problem-204](src/main/java/in/ashwanik/dcp/problems/p181_210/p204):sunny:
32173217

32183218

32193219
> This problem was asked by Amazon.

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ Solutions to the coding problems from [Daily coding problem](https://dailycoding
2121
|[P190](src/main/java/in/ashwanik/dcp/problems/p181_210/p190)|[P194](src/main/java/in/ashwanik/dcp/problems/p181_210/p194)|[P199](src/main/java/in/ashwanik/dcp/problems/p181_210/p199)|
2222

2323

24-
## **Amazon (18)**
24+
## **Amazon (19)**
2525
| | | | | | | | | | | | |
2626
|--|--|--|--|--|--|--|--|--|--|--|--|
2727
|[P12](src/main/java/in/ashwanik/dcp/problems/p1_30/p12)|[P13](src/main/java/in/ashwanik/dcp/problems/p1_30/p13)|[P29](src/main/java/in/ashwanik/dcp/problems/p1_30/p29)|[P43](src/main/java/in/ashwanik/dcp/problems/p31_60/p43)|[P46](src/main/java/in/ashwanik/dcp/problems/p31_60/p46)|[P49](src/main/java/in/ashwanik/dcp/problems/p31_60/p49)|[P57](src/main/java/in/ashwanik/dcp/problems/p31_60/p57)|[P58](src/main/java/in/ashwanik/dcp/problems/p31_60/p58)|[P65](src/main/java/in/ashwanik/dcp/problems/p61_90/p65)|[P84](src/main/java/in/ashwanik/dcp/problems/p61_90/p84)|[P133](src/main/java/in/ashwanik/dcp/problems/p121_150/p133)|[P137](src/main/java/in/ashwanik/dcp/problems/p121_150/p137)|
28-
|[P143](src/main/java/in/ashwanik/dcp/problems/p121_150/p143)|[P154](src/main/java/in/ashwanik/dcp/problems/p151_180/p154)|[P157](src/main/java/in/ashwanik/dcp/problems/p151_180/p157)|[P171](src/main/java/in/ashwanik/dcp/problems/p151_180/p171)|[P184](src/main/java/in/ashwanik/dcp/problems/p181_210/p184)|[P197](src/main/java/in/ashwanik/dcp/problems/p181_210/p197)|
28+
|[P143](src/main/java/in/ashwanik/dcp/problems/p121_150/p143)|[P154](src/main/java/in/ashwanik/dcp/problems/p151_180/p154)|[P157](src/main/java/in/ashwanik/dcp/problems/p151_180/p157)|[P171](src/main/java/in/ashwanik/dcp/problems/p151_180/p171)|[P184](src/main/java/in/ashwanik/dcp/problems/p181_210/p184)|[P197](src/main/java/in/ashwanik/dcp/problems/p181_210/p197)|[P204](src/main/java/in/ashwanik/dcp/problems/p181_210/p204)|
2929

3030

3131
## **Microsoft (16)**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Given a complete binary tree, count the number of nodes in faster than O(n)
2+
time. Recall that a complete binary tree has every level filled except the last,
3+
and the nodes in the last level are filled starting from the left.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package in.ashwanik.dcp.problems.p181_210.p204;
2+
3+
import in.ashwanik.dcp.common.TreeNode;
4+
5+
class Solution {
6+
7+
int numberOfNodes(TreeNode<Integer> root) {
8+
if (root == null) {
9+
return 0;
10+
}
11+
12+
int leftHeight = getLeftHeight(root) + 1;
13+
int righHeight = getRightHeight(root) + 1;
14+
if (leftHeight == righHeight) {
15+
return (int) Math.pow(2, leftHeight) - 1;
16+
}
17+
18+
return 1 + numberOfNodes(root.getLeft()) + numberOfNodes(root.getRight());
19+
20+
}
21+
22+
private int getLeftHeight(TreeNode<Integer> root) {
23+
int height = 0;
24+
if (root == null) {
25+
return height;
26+
}
27+
while (root.getLeft() != null) {
28+
root = root.getLeft();
29+
height++;
30+
}
31+
32+
return height;
33+
}
34+
35+
private int getRightHeight(TreeNode<Integer> root) {
36+
int height = 0;
37+
if (root == null) {
38+
return height;
39+
}
40+
while (root.getRight() != null) {
41+
root = root.getRight();
42+
height++;
43+
}
44+
45+
return height;
46+
}
47+
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package in.ashwanik.dcp.problems.p181_210.p204;
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 testCountNodes() {
11+
TreeNode<Integer> root = new TreeNode<>(1
12+
, new TreeNode<>(2
13+
, new TreeNode<>(4)
14+
, new TreeNode<>(5))
15+
, new TreeNode<>(3));
16+
17+
assertEquals(5, new Solution().numberOfNodes(root));
18+
}
19+
}

0 commit comments

Comments
(0)

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