-
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
d6320a2
commit 42ef843
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/com/pallamsetty/trees/BinaryTreeRightSideView.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,44 @@ | ||
package com.pallamsetty.trees; | ||
|
||
/* | ||
* Leetcode 199 | ||
* */ | ||
|
||
import com.pallamsetty.trees.helpers.TreeNode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
|
||
public class BinaryTreeRightSideView { | ||
public List<Integer> getRightSideView(TreeNode root) { | ||
if(root == null) { | ||
return new ArrayList<>(); | ||
} | ||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.offer(root); | ||
List<Integer> result = new ArrayList<>(); | ||
|
||
while(!queue.isEmpty()) { | ||
int size = queue.size(); | ||
|
||
for(int i = 0; i < size; i++) { | ||
TreeNode current = queue.poll(); | ||
|
||
if(i == size - 1) { | ||
result.add(current.val); | ||
} | ||
|
||
if(current.left != null) { | ||
queue.offer(current.left); | ||
} | ||
|
||
if(current.right != null) { | ||
queue.offer(current.right); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/test/java/com/pallamsetty/trees/BinaryTreeRightSideViewTest.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,95 @@ | ||
package com.pallamsetty.trees; | ||
|
||
import com.pallamsetty.trees.helpers.TreeNode; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
|
||
public class BinaryTreeRightSideViewTest { | ||
private final BinaryTreeRightSideView btrsv; | ||
|
||
public BinaryTreeRightSideViewTest() { | ||
btrsv = new BinaryTreeRightSideView(); | ||
} | ||
|
||
@Test | ||
public void testGetRightSideView1() { | ||
TreeNode root = new TreeNode(1); | ||
root.left = new TreeNode(2); | ||
root.right = new TreeNode(3); | ||
|
||
List<Integer> expected = Arrays.asList(new Integer[] {1, 3}); | ||
List<Integer> actual = btrsv.getRightSideView(root); | ||
|
||
assertArrayEquals(expected.toArray(), actual.toArray()); | ||
} | ||
|
||
@Test | ||
public void testGetRightSideView2() { | ||
TreeNode root = new TreeNode(1); | ||
root.left = new TreeNode(2); | ||
root.left.left = new TreeNode(4); | ||
root.left.right = new TreeNode(5); | ||
root.right = new TreeNode(3); | ||
root.right.left = new TreeNode(6); | ||
root.right.right = new TreeNode(7); | ||
|
||
List<Integer> expected = Arrays.asList(new Integer[] {1, 3, 7}); | ||
List<Integer> actual = btrsv.getRightSideView(root); | ||
|
||
assertArrayEquals(expected.toArray(), actual.toArray()); | ||
} | ||
|
||
@Test | ||
public void testGetRightSideView3() { | ||
TreeNode root = new TreeNode(1); | ||
root.left = new TreeNode(2); | ||
root.left.right = new TreeNode(5); | ||
root.right = new TreeNode(3); | ||
root.right.right = new TreeNode(4); | ||
|
||
List<Integer> expected = Arrays.asList(new Integer[] {1, 3, 4}); | ||
List<Integer> actual = btrsv.getRightSideView(root); | ||
|
||
assertArrayEquals(expected.toArray(), actual.toArray()); | ||
} | ||
|
||
@Test | ||
public void testGetRightSideView4() { | ||
TreeNode root = new TreeNode(1); | ||
root.left = new TreeNode(2); | ||
root.left.left = new TreeNode(4); | ||
root.right = new TreeNode(3); | ||
root.left.left.left = new TreeNode(5); | ||
|
||
List<Integer> expected = Arrays.asList(new Integer[] {1, 3, 4, 5}); | ||
List<Integer> actual = btrsv.getRightSideView(root); | ||
|
||
assertArrayEquals(expected.toArray(), actual.toArray()); | ||
} | ||
|
||
@Test | ||
public void testGetRightSideView5() { | ||
TreeNode root = new TreeNode(1); | ||
root.right = new TreeNode(3); | ||
|
||
List<Integer> expected = Arrays.asList(new Integer[] {1, 3}); | ||
List<Integer> actual = btrsv.getRightSideView(root); | ||
|
||
assertArrayEquals(expected.toArray(), actual.toArray()); | ||
} | ||
|
||
@Test | ||
public void testGetRightSideView6() { | ||
TreeNode root = null; | ||
|
||
List<Integer> expected = new ArrayList<>(); | ||
List<Integer> actual = btrsv.getRightSideView(root); | ||
|
||
assertArrayEquals(expected.toArray(), actual.toArray()); | ||
} | ||
} |