Binary Tree Right Side View

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024

Комментарии • 90

  • @tannerbarcelos6880
    @tannerbarcelos6880 3 года назад +3

    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.

  • @kaushal136
    @kaushal136 4 года назад +6

    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

  • @jimmyjose99
    @jimmyjose99 3 года назад +1

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

  • @psn999100
    @psn999100 5 лет назад +3

    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 ?

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      Thanks Pradosh and anytime, I'm always happy to help! I'd love to hear your ideas!

  • @michaeldang8189
    @michaeldang8189 4 года назад +1

    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

    • @dorlg6655
      @dorlg6655 2 года назад

      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

  • @indranilthakur3605
    @indranilthakur3605 4 года назад +20

    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?

    • @sridhargana155
      @sridhargana155 3 года назад

      something like Bottom to Top

    • @AnimeshPaul23
      @AnimeshPaul23 3 года назад +1

      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.

  • @mayanksharma9827
    @mayanksharma9827 3 года назад

    You are seriously one of the best teacher

  • @huizhao2050
    @huizhao2050 2 года назад

    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)

  • @tadiarvindraju2157
    @tadiarvindraju2157 4 года назад +2

    Bro really appreciate it!! Never really got bfs till now atleast not till i watched your awesome vid ;)

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад +1

      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!

  • @surajgrandhi6742
    @surajgrandhi6742 4 года назад

    Very well explained brother ....
    Great work
    Love from india

  • @jinxscript
    @jinxscript 2 года назад

    need more of this man 💔

  • @divyanshuchaudhari5416
    @divyanshuchaudhari5416 3 года назад +1

    Effortless..thanks for all your videos..It really helps.

  • @TrollFreeInternet
    @TrollFreeInternet 5 лет назад +22

    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?

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад +30

      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!

    • @sannge6471
      @sannge6471 4 года назад +5

      i did leetcode for 2 weeks, and these problems are so easy for me now.

  • @hermanyniu2292
    @hermanyniu2292 5 лет назад +4

    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?

  • @paullin7636
    @paullin7636 5 лет назад +4

    Very nice content thank you so much for the video.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      Thanks Paul and anytime! Thank YOU for your support!

  • @mohammedalmukhtar8949
    @mohammedalmukhtar8949 4 года назад +2

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

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад +1

      Anytime Mohammed thanks for the support I really appreciate it!!!

  • @coderbehindthescreen9336
    @coderbehindthescreen9336 2 года назад

    loving your approach :-)

  • @treyquattro
    @treyquattro 4 года назад

    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!

  • @AnimeshPaul23
    @AnimeshPaul23 3 года назад

    Great explanation.

  • @arno.claude
    @arno.claude Год назад

    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.

  • @aribasiebel
    @aribasiebel 5 лет назад +3

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

  • @nileshsharma5377
    @nileshsharma5377 4 года назад

    Index at 2^(n-1) where n>0will give results

  • @GauravKumar-by1yt
    @GauravKumar-by1yt 5 лет назад +2

    Thanks Kevin Sir

  • @saipavanpurella9253
    @saipavanpurella9253 3 года назад

    man couldn't find a teacher better than you

  • @Uber_handle
    @Uber_handle 3 года назад

    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/

  • @HardikShah13
    @HardikShah13 4 года назад +3

    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?

    • @ajaybiswas22
      @ajaybiswas22 2 года назад +3

      left subtree can have depth longer than right subtree

  • @danni6113
    @danni6113 5 лет назад +4

    Great work!

  • @bauminds6304
    @bauminds6304 5 лет назад +1

    Thanks for your explanation Kevin!! Could you please help explain problem 528?

  • @Nobody2310
    @Nobody2310 5 лет назад +3

    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?

    • @unknownman1
      @unknownman1 3 года назад +1

      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.

  • @pankajrathi
    @pankajrathi 3 года назад

    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);
    }
    }
    }

  • @deepakdas4513
    @deepakdas4513 4 года назад

    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.

  • @satheshbm92
    @satheshbm92 4 года назад +1

    Perfect. Thank you Kevin :)

  • @dinsummer535
    @dinsummer535 3 года назад

    Nice simple explanation :)

  • @sumitthakur7526
    @sumitthakur7526 3 года назад

    You made me love tree!!!!

  • @lavishgarg4274
    @lavishgarg4274 4 года назад

    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

    • @princemoronfolu
      @princemoronfolu 4 года назад

      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

  • @flavorfabs
    @flavorfabs 5 лет назад +2

    Well done dude

  • @kevindebruyne17
    @kevindebruyne17 3 года назад

    Amazing explanation, keep it up!

  • @MohammedJavid-jz8vi
    @MohammedJavid-jz8vi 5 лет назад +2

    Thank you so much sir keep helping us may Allah bless you 😘🙌

  • @mahmoudmaamoun6422
    @mahmoudmaamoun6422 4 года назад

    Thank you Kevin your solutions are easy and elegant!
    Why does it matter to go from left to right and not right to left?

    • @adistutorials5399
      @adistutorials5399 4 года назад

      If you go right to left then you can use the first node in the queue to add to answer list

  • @suriveer
    @suriveer 3 года назад

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

  • @nosoyyotampoco
    @nosoyyotampoco 4 года назад

    Great explanation!!!

  • @akki_gamingzone
    @akki_gamingzone 3 года назад

    can you please explain why you are checking left childs as we want right side view only?

  • @sharpnova2
    @sharpnova2 5 лет назад +5

    why not just do a simple level wise traversal with a queue and continually add the rightmost (last) element for each level?

    • @JM_utube
      @JM_utube 4 года назад +6

      that's exactly what he's doing

    • @unknownman1
      @unknownman1 3 года назад

      @@JM_utube lol. true

  • @dorlg6655
    @dorlg6655 2 года назад

    Hey Kevin, how do you decide to use bfs recursively or iteratively?

  • @sreejith5966
    @sreejith5966 3 года назад

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

  • @vyshnavig1723
    @vyshnavig1723 3 года назад

    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

  • @2awesome4u2000
    @2awesome4u2000 3 года назад

    I got asked this question in Jan 2021

  • @sharkk2979
    @sharkk2979 3 года назад

    Why u shave, the way u shave?

  • @cnaught0802
    @cnaught0802 5 лет назад +1

    So helpful!!

  • @vk1618
    @vk1618 4 года назад

    Start woth a sanity check

  • @aj9706
    @aj9706 5 лет назад

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

  • @ishanrtripathi
    @ishanrtripathi 5 лет назад

    Thanks

  • @julietgeorge4858
    @julietgeorge4858 4 года назад

    God, you are amazing!

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад

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

  • @ChandrashekharMuradnar
    @ChandrashekharMuradnar 4 года назад

    thanks

  • @jeffmason613
    @jeffmason613 5 лет назад

    Are you already working at a large tech company or are you currently applying?

  • @ashokbalakrishnan1371
    @ashokbalakrishnan1371 4 года назад

    You are really veryy cool!!!!

  • @Zen-lf7zr
    @Zen-lf7zr 5 лет назад

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

  • @alanchavez16
    @alanchavez16 5 лет назад

    Hey Kevin. Do you think you can help me to solve one problem?

  • @rohankulkarni7081
    @rohankulkarni7081 2 года назад +1

    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);
    }
    }

  • @MrPlanes747
    @MrPlanes747 5 лет назад

    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!

  • @senthilathii
    @senthilathii 4 года назад +1

    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;

  • @my3m
    @my3m 4 года назад

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

  • @surajgrandhi6742
    @surajgrandhi6742 4 года назад

    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