Skip to content
51 changes: 51 additions & 0 deletions Java/binaryTreeRightSideView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
/*
Time Complexity : O(n)
Space Complexity : O(n)

Recursive call is made to right subtree and then the left subtree
Note : if current level is greater than the max level we have visited rightmost node
*/

class Solution {
static int maxLevel;
public static List<Integer> rightSideView(TreeNode root) {

maxLevel = 0;
int level = 1;
List<Integer> result = new ArrayList<Integer>();
rightSideViewHelper(root,level,result);

return result;
}
public static void rightSideViewHelper(TreeNode root,int level,List<Integer> result)
{
if(root == null)
{
return;
}

if(maxLevel < level)
{
maxLevel = level;
result.add(root.val);
}

rightSideViewHelper(root.right,level+1,result);
rightSideViewHelper(root.left,level+1,result);
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
| [Aysia](https://www.linkedin.com/in/aysiaelise/) <br> <img src="https://avatars.githubusercontent.com/u/70167431?s=460&u=1637be8636b6db6e35343ed9c1318c23e909b463&v=4" width="100" height="100"> | USA | JavaScript | [GitHub](https://github.com/aysiae) |
| [Poorvi Garg](https://github.com/POORVI111) <br> <img src="https://avatars.githubusercontent.com/u/68559217?s=400&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/POORVI111) |
| [Lakshmanan Meiyappan](https://laxmena.com) <br> <img src="https://avatars.githubusercontent.com/u/12819059?s=400&v=4" width="100" height="100"> | India | C++ | [Website - Blog](https://laxmena.com)<br/> [GitHub](https://github.com/laxmena) <br/> [LinekdIn](https://www.linkedin.com/in/lakshmanan-meiyappan/) |

| Carolin James](https://github.com/Carolin16) <br> <img src="" width="" height="">
| India | Java |[GitHub](https://github.com/Carolin16)
|
<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down