Your strategy of holding onto one algorithm and using it in lot of similar questions helps me a lot to code efficiently .. thank you so much.. keep making such cool videos
Thats excellent Kevin. This exact question was asked recently in Uber Phone interview and you explained it flawlessly. Thanks a lot. I have been liking your videos a lot . I really appreciate how you take out the time of the day and explain this to us while also working a full-time job. Bravo ! Looking for more videos to come. if youre open to ideas , do you want me to share some ideas for your channel wrt leetcode ?
Since the input is an array representation of a tree, each level of the tree would be in this pattern: Level 0(root) : index 0 Level 1 : index 1 to 2 Level 2 : index 3 to 6 Level 3 : index 7 to 14 Level 4 : index 15 to 30 etc. Therefore, an alternative way is to iterate through each level of indices backward to find the first non-null value and add it to the result list to return. for (int level = 0; level < log2(array.length) + 1; level++) { for (int i = ((1 = ((1
When you said top to bottom, I thought maybe you'll talk about DFS as it goes deep and BFS goes wide. Didn't know that tip to bottom means BFS. What would be the hint to look for DFS?
Arvind Raju anytime! Check out the interviewing service I created if you like my explanations! thedailybyte.dev/?ref=kevin I recommend joining a premium plan if you can!
Haha don't be Pat!!! These questions are really hard and I definitely struggle with these too (especially when I first started). Just keep practicing and it will keep getting easier!
Hi Kevin! I did it in the same way. Just a quick question tho- I saw it on LeetCode that some people say interviewers prefer that you answer this type of question with dfs using recursion. Is that true based on your experience?
I thought this was a badly described problem. The way I understood it initially is looking at the tree in plan view so you just do a regular DFS of the right side. Obviously that's not a Medium level LC problem!
Good video, Kevin. Though this approach should be O(1) space. The output doesn't count towards the space complexity, and the queue has at most 4 elements at any given time.
I was thinking about dfs for building depth on the right subtree, but if it makes more sense to use bfs because if a left child isn't obstructed on the right side you would be able to process it right away/
Hey Kevin, Would it be easily solved by modified "PreOrder"? You will only traverse right and keep adding value to the list and return it. What is the reason to do BFS and traverse all left and right node when we know we need to ignore all left node?
I think other way also be to add right first and then left. And then when we pop out from queue just add that. That would be the right most one or the only one if size is 1. Isn’t it?
nope. If you'll add right first in the queue and then left, then your right will be removed first since it is queue(FIFO) and therefore right will be the first element to be removed which fails line 23.
Instead of adding the value in the for loop you can add it before the for loop provided you are adding the right subtree first and left subtree after it. Here is my code: while (queue.Count != 0) { int size = queue.Count; result.Add(queue.Peek().val); for (int i = 0; i < size; i++) { var current = queue.Dequeue(); if (current.right != null) { queue.Enqueue(current.right); } if (current.left != null) { queue.Enqueue(current.left); } } }
Awesome explanation. I hope someday I will be able to solve problems as fast as you.Also I have a Google phone interview in a week. What type of questions they ask in this round?I think I will have to write code in Google doc.
It's the simple question, but damn ,the logic doesn't clicked to me that is i==size-1😕,thanks Kevin,your channel is underrated if we compare with the benefit it giving to many students
Y can't we simply print 2n+2(right node) value of index o and then applying same for current right node using while loop as they have mentioned it is a binary tree
Juliet George thanks Juliet! If you need help with these problems sign up for the interviewing service I made thedailybyte.dev?ref=kevin I recommend joining the annual tier :)
What is wrong with this solution? class Solution { public List rightSideView(TreeNode root) { if(root==null){ List ans=new ArrayList(); return ans; } List ans=new ArrayList(); rightS(root, ans); return ans; } public static void rightS(TreeNode root, List ans){ if(root==null){ return; } if(root.right==null && root.left==null){ ans.add(root.val); return; }else if(root.right==null){ ans.add(root.val); rightS(root.left,ans); } ans.add(root.val); rightS(root.right,ans); } }
Are these questions that FB will continue to use throughout their interview process? Your videos say (2019). So if we are applying for Summer 2020, will FB and other companies start using other problems? Thanks!
Below code works fine - still I'm wondering - do we need to loop through queue since we i avoid if we not consider left side node. while (queue.Count != 0) { TreeNode current = queue.Dequeue(); if (current.right != null) { queue.Enqueue(current.right); } } return visibleValues;
I went with a temp array and then sliced off the [-1] element using a while loop . This saves some space. Great catch and solution.
Your strategy of holding onto one algorithm and using it in lot of similar questions helps me a lot to code efficiently .. thank you so much.. keep making such cool videos
Great tip regarding BFS approach being generally used for "top to bottom" cases!
Thats excellent Kevin. This exact question was asked recently in Uber Phone interview and you explained it flawlessly. Thanks a lot. I have been liking your videos a lot . I really appreciate how you take out the time of the day and explain this to us while also working a full-time job. Bravo ! Looking for more videos to come. if youre open to ideas , do you want me to share some ideas for your channel wrt leetcode ?
Thanks Pradosh and anytime, I'm always happy to help! I'd love to hear your ideas!
Since the input is an array representation of a tree, each level of the tree would be in this pattern:
Level 0(root) : index 0
Level 1 : index 1 to 2
Level 2 : index 3 to 6
Level 3 : index 7 to 14
Level 4 : index 15 to 30
etc.
Therefore, an alternative way is to iterate through each level of indices backward to find the first non-null value and add it to the result list to return.
for (int level = 0; level < log2(array.length) + 1; level++) {
for (int i = ((1 = ((1
in leet code thats the way they describe a tree, but as you can see the input is not an array, but a tree node
When you said top to bottom, I thought maybe you'll talk about DFS as it goes deep and BFS goes wide. Didn't know that tip to bottom means BFS. What would be the hint to look for DFS?
something like Bottom to Top
DFS moves from top to bottom and backtracks from bottom to top. By "top to bottom", I think what he meant was a unidirectional traversal.
You are seriously one of the best teacher
If you change the question from stand up on the right to the left, then you just need to change one line code. if(i==size-1) ==> if(i == 0)
Bro really appreciate it!! Never really got bfs till now atleast not till i watched your awesome vid ;)
Arvind Raju anytime! Check out the interviewing service I created if you like my explanations! thedailybyte.dev/?ref=kevin I recommend joining a premium plan if you can!
Very well explained brother ....
Great work
Love from india
need more of this man 💔
Effortless..thanks for all your videos..It really helps.
Dude your solutions make me depressed lol..do u just come up with such elegant solutions or do u struggle aswell? How long have u been leetcoding?
Haha don't be Pat!!! These questions are really hard and I definitely struggle with these too (especially when I first started). Just keep practicing and it will keep getting easier!
i did leetcode for 2 weeks, and these problems are so easy for me now.
Hi Kevin! I did it in the same way. Just a quick question tho- I saw it on LeetCode that some people say interviewers prefer that you answer this type of question with dfs using recursion. Is that true based on your experience?
Very nice content thank you so much for the video.
Thanks Paul and anytime! Thank YOU for your support!
Kevin! You are a f** badass dude! thank you so much!!
Anytime Mohammed thanks for the support I really appreciate it!!!
loving your approach :-)
I thought this was a badly described problem. The way I understood it initially is looking at the tree in plan view so you just do a regular DFS of the right side. Obviously that's not a Medium level LC problem!
Great explanation.
Good video, Kevin. Though this approach should be O(1) space. The output doesn't count towards the space complexity, and the queue has at most 4 elements at any given time.
Nice and concise explanation ... just subscribed. I posted my version inspired by you. Keep it up Kevin!
Thanks Ramon!!!
Index at 2^(n-1) where n>0will give results
Thanks Kevin Sir
Anytime Gaurav!
man couldn't find a teacher better than you
I was thinking about dfs for building depth on the right subtree, but if it makes more sense to use bfs because if a left child isn't obstructed on the right side you would be able to process it right away/
Hey Kevin, Would it be easily solved by modified "PreOrder"? You will only traverse right and keep adding value to the list and return it. What is the reason to do BFS and traverse all left and right node when we know we need to ignore all left node?
left subtree can have depth longer than right subtree
Great work!
Thanks!
Thanks for your explanation Kevin!! Could you please help explain problem 528?
I think other way also be to add right first and then left. And then when we pop out from queue just add that. That would be the right most one or the only one if size is 1. Isn’t it?
nope. If you'll add right first in the queue and then left, then your right will be removed first since it is queue(FIFO) and therefore right will be the first element to be removed which fails line 23.
Instead of adding the value in the for loop you can add it before the for loop provided you are adding the right subtree first and left subtree after it.
Here is my code:
while (queue.Count != 0)
{
int size = queue.Count;
result.Add(queue.Peek().val);
for (int i = 0; i < size; i++)
{
var current = queue.Dequeue();
if (current.right != null)
{
queue.Enqueue(current.right);
}
if (current.left != null)
{
queue.Enqueue(current.left);
}
}
}
Awesome explanation. I hope someday I will be able to solve problems as fast as you.Also I have a Google phone interview in a week. What type of questions they ask in this round?I think I will have to write code in Google doc.
Perfect. Thank you Kevin :)
Anytime :)
Nice simple explanation :)
You made me love tree!!!!
It's the simple question, but damn ,the logic doesn't clicked to me that is i==size-1😕,thanks Kevin,your channel is underrated if we compare with the benefit it giving to many students
Lavish Garg I == size -1 because it’s zero based and it starts from 0. If the size is 4, it goes from 0-3
Well done dude
Thanks Fabio!
Amazing explanation, keep it up!
Thank you so much sir keep helping us may Allah bless you 😘🙌
Anytime Javid!
Thank you Kevin your solutions are easy and elegant!
Why does it matter to go from left to right and not right to left?
If you go right to left then you can use the first node in the queue to add to answer list
great explanation. Keep up the good work. subscribed.. like had a option :)
Great explanation!!!
thanks!
can you please explain why you are checking left childs as we want right side view only?
why not just do a simple level wise traversal with a queue and continually add the rightmost (last) element for each level?
that's exactly what he's doing
@@JM_utube lol. true
Hey Kevin, how do you decide to use bfs recursively or iteratively?
Level order
Left view
Right view are almost the same with minor change
Y can't we simply print 2n+2(right node) value of index o and then applying same for current right node using while loop as they have mentioned it is a binary tree
I got asked this question in Jan 2021
Why u shave, the way u shave?
So helpful!!
Thanks Court!
Start woth a sanity check
Could u pls make a video on validate binary search tree?
Thanks
God, you are amazing!
Juliet George thanks Juliet! If you need help with these problems sign up for the interviewing service I made thedailybyte.dev?ref=kevin I recommend joining the annual tier :)
thanks
Are you already working at a large tech company or are you currently applying?
You are really veryy cool!!!!
haha thanks!!!
HOLD THE FUCK UP! You're actually using Firefox????? I thought you used Chrome.
Hey Kevin. Do you think you can help me to solve one problem?
What is wrong with this solution?
class Solution {
public List rightSideView(TreeNode root) {
if(root==null){
List ans=new ArrayList();
return ans;
}
List ans=new ArrayList();
rightS(root, ans);
return ans;
}
public static void rightS(TreeNode root, List ans){
if(root==null){
return;
}
if(root.right==null && root.left==null){
ans.add(root.val);
return;
}else if(root.right==null){
ans.add(root.val);
rightS(root.left,ans);
}
ans.add(root.val);
rightS(root.right,ans);
}
}
Are these questions that FB will continue to use throughout their interview process? Your videos say (2019). So if we are applying for Summer 2020, will FB and other companies start using other problems? Thanks!
Below code works fine - still I'm wondering - do we need to loop through queue since we i avoid if we not consider left side node.
while (queue.Count != 0)
{
TreeNode current = queue.Dequeue();
if (current.right != null)
{
queue.Enqueue(current.right);
}
}
return visibleValues;
dfs solution is more short and concise imo. only 8 lines of code.
class Solution {
public List rightSideView(TreeNode root) {
List list = new ArrayList();
if(root == null){
return list;
}
Queue q = new LinkedList();
q.add(root);
while(!q.isEmpty()){
int size = q.size();
for(int i =0 ;i