Leetcode 199 Binary Tree Right Side View (Medium)
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Data Structure & Algorithms : the most important and fundamental of any programmer. We will explore famous questions on datastructure & algorithms from various popular websites.
Let's discuss problem no 235 at leetcode i.e 235. Lowest Common Ancestor of a Binary Search Tree. Problem Link: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ Here we are supposed to find the lowest common ancestor in ...
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
Question Link : https://leetcode.com/problems/binary-tree-right-side-view/
It is pretty simple explanation if you visualize the tree .If we do level order traversal and keep each level node values in a list (keeping from left to right) the last value in the list needs to be added to result as that is the only one which will be seen from right side view. In case of the left side view it will be the first value in list of node for the level or if we keep nodes from right to left it will still be the last node value of each level.
Since in this question we have right side view , lets see the list content per level for the given sample in the question
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <--- Level 1
/ \
2 3 <--- Level 2
\ \
5 4 <--- Level 3
Content of the level wise value will be
Level 1 -> [1]
Level 2-> [2,3]
Level 3 -> [5,4]
The last value of each level needs to be added for result view.
Now we can simply write a Level Order Traversal using Queue
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res=new ArrayList<>();
List<Integer> level=new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
if(root==null) return res;
q.offer(root);
q.offer(null);
while(!q.isEmpty()){
TreeNode tmp= q.poll();
if(tmp!=null){
level.add(tmp.val);
if(tmp.left!=null)
q.offer(tmp.left);
if(tmp.right!=null)
q.offer(tmp.right);
}else{
if(level.size()>0)
res.add(level.get(level.size()-1));
level.clear();
if(!q.isEmpty())
q.offer(null);
}
}
return res;
}
}