An open API service indexing awesome lists of open source software.

https://github.com/emahtab/binary-tree-right-side-view

Binary Tree Right Side View
https://github.com/emahtab/binary-tree-right-side-view

bfs binary-tree leetcode problem-solving

Last synced: 4 months ago
JSON representation

Binary Tree Right Side View

Awesome Lists containing this project

README

        

# Binary Tree Right Side View
## https://leetcode.com/problems/binary-tree-right-side-view

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

```
Example:
Given the below tree, right side view of the tree will be : [1, 3, 4, 6]
Explanation:

1 <---
/ \
2 3 <---
\ \
5 4 <---
/
6 <---
```

## Implementation :

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List rightSideView(TreeNode root) {
List list = new ArrayList<>();
if(root == null)
return list;
Queue queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
TreeNode current = queue.poll();
if(i == size - 1)
list.add(current.val);
if(current.left != null)
queue.offer(current.left);
if(current.right != null)
queue.offer(current.right);
}
}
return list;
}
}
```

```
Runtime Complexity = O(n), where n is the total number of nodes in the binary tree
Space Complexity = O(n), where n is the total number of nodes in the binary tree
```

# References :
https://www.youtube.com/watch?v=jCqIr_tBLKs