Binary Tree Right Side View

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

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.

tannerbarcelos
Автор

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

kaushal
Автор

Great tip regarding BFS approach being generally used for "top to bottom" cases!

jimmyjose
Автор

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 ?

psn
Автор

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 << (level + 1)) - 2); i >= ((1 << level) - 1); i--) {
if (array[i] != null) {
result.add(array[i]);
break;
}
}
}

michaeldang
Автор

You are seriously one of the best teacher

mayanksharma
Автор

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)

huizhao
Автор

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?

TrollFreeInternet
Автор

Kevin! You are a f** badass dude! thank you so much!!

mohammedalmukhtar
Автор

Nice and concise explanation ... just subscribed. I posted my version inspired by you. Keep it up Kevin!

aribasiebel
Автор

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.

deepakdas
Автор

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/

githendumukiri
Автор

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;

for (int i = 0; i < size; i++)
{
var current = queue.Dequeue();
if (current.right != null)
{

}

if (current.left != null)
{
queue.Enqueue(current.left);
}
}
}

pankajrathi
Автор

Level order
Left view
Right view are almost the same with minor change

sreejith
Автор

great explanation. Keep up the good work. subscribed.. like had a option :)

suriveer
Автор

Could u pls make a video on validate binary search tree?

aj
Автор

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)
{

}
}

return visibleValues;

senthilathii
Автор

HOLD THE FUCK UP! You're actually using I thought you used Chrome.

Zen-lfzr
Автор

dfs solution is more short and concise imo. only 8 lines of code.

mym
visit shbcf.ru