Binary Tree Right Side View

public List<Integer> rightSideView(TreeNode root) {

return rightSideViewBFS(root);
}

private List<Integer> rightSideViewBFS(TreeNode root){

List<Integer> result = new ArrayList<Integer>();

if(root==null){
return result;
}

Queue<TreeNode> queue = new LinkedList<TreeNode>();

queue.add(root);

while(queue.size() != 0){
int size = queue.size();

for (int i=0; i<size; i++) {

TreeNode node = queue.remove();

if(i==0){
result.add(node.val);
}

if(node.right!=null){
queue.add(node.right);
}

if(node.left!=null){
queue.add(node.left);
}

}
}

return result;

}

--

--

--

Exploring and discovering. Learning everyday.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

iOS TTS (Text To Speech)

UICollectionView in iOS

Control Yeelight Lightstrip Plus with Home Assistant

Publishing Apps with Apple Business Manager

How to use Cupertino icons in Flutter

print vs NSLog

Convert Array into Json String

How to manage CollectionView cell size swift

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Smrita Pokharel

Smrita Pokharel

Exploring and discovering. Learning everyday.

More from Medium

SOLID Design in Practice — S: Single Responsibility Principle

Design Patterns — Prototype Pattern

SOLID Principles for Maintainable Code

VERY Basic Differences Between Java and JavaScript-Arrays