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
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!
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 ?
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?
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/
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?
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
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!
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?
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 :)
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.
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!
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
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 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); } } }
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
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;
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); } }
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!
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!
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!
Very well explained brother ....
Great work
Love from india
loving your approach :-)
You are seriously one of the best teacher
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.
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/
need more of this man 💔
man couldn't find a teacher better than you
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?
Amazing explanation, keep it up!
Great explanation.
You made me love tree!!!!
Nice simple explanation :)
Perfect. Thank you Kevin :)
Anytime :)
Kevin! You are a f** badass dude! thank you so much!!
Anytime Mohammed thanks for the support I really appreciate it!!!
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)
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
Great work!
Thanks!
Thanks for your explanation Kevin!! Could you please help explain problem 528?
Very nice content thank you so much for the video.
Thanks Paul and anytime! Thank YOU for your support!
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.
Hey Kevin, how do you decide to use bfs recursively or iteratively?
great explanation. Keep up the good work. subscribed.. like had a option :)
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
Nice and concise explanation ... just subscribed. I posted my version inspired by you. Keep it up Kevin!
Thanks Ramon!!!
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 :)
Effortless..thanks for all your videos..It really helps.
can you please explain why you are checking left childs as we want right side view only?
Great explanation!!!
thanks!
So helpful!!
Thanks Court!
Thanks Kevin Sir
Anytime Gaurav!
Well done dude
Thanks Fabio!
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.
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!
Are you already working at a large tech company or are you currently applying?
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
Why u shave, the way u shave?
Thanks
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
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.
Hey Kevin. Do you think you can help me to solve one problem?
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.
thanks
Could u pls make a video on validate binary search tree?
Index at 2^(n-1) where n>0will give results
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);
}
}
}
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
Thank you so much sir keep helping us may Allah bless you 😘🙌
Anytime Javid!
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
I got asked this question in Jan 2021
Start woth a sanity check
You are really veryy cool!!!!
haha thanks!!!
Level order
Left view
Right view are almost the same with minor change
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;
HOLD THE FUCK UP! You're actually using Firefox????? I thought you used Chrome.
dfs solution is more short and concise imo. only 8 lines of code.
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);
}
}
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