Binary Tree Right Side View

แชร์
ฝัง

ความคิดเห็น • 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!

  • @mayanksharma9827
    @mayanksharma9827 3 ปีที่แล้ว

    You are seriously one of the best teacher

  • @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.

  • @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 💔

  • @saipavanpurella9253
    @saipavanpurella9253 3 ปีที่แล้ว

    man couldn't find a teacher better than you

  • @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)

  • @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.

  • @coderbehindthescreen9336
    @coderbehindthescreen9336 2 ปีที่แล้ว

    loving your approach :-)

  • @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?

  • @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!!!

  • @divyanshuchaudhari5416
    @divyanshuchaudhari5416 3 ปีที่แล้ว +1

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

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

  • @bauminds6304
    @bauminds6304 5 ปีที่แล้ว +1

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

  • @AnimeshPaul23
    @AnimeshPaul23 3 ปีที่แล้ว

    Great explanation.

  • @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!

  • @aribasiebel
    @aribasiebel 5 ปีที่แล้ว +3

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

  • @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.

  • @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!

  • @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/

  • @danni6113
    @danni6113 5 ปีที่แล้ว +4

    Great work!

  • @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.

  • @nileshsharma5377
    @nileshsharma5377 4 ปีที่แล้ว

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

  • @sumitthakur7526
    @sumitthakur7526 3 ปีที่แล้ว

    You made me love tree!!!!

  • @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.

  • @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

  • @satheshbm92
    @satheshbm92 4 ปีที่แล้ว +1

    Perfect. Thank you Kevin :)

  • @flavorfabs
    @flavorfabs 5 ปีที่แล้ว +2

    Well done dude

  • @GauravKumar-by1yt
    @GauravKumar-by1yt 5 ปีที่แล้ว +2

    Thanks Kevin Sir

  • @dinsummer535
    @dinsummer535 3 ปีที่แล้ว

    Nice simple explanation :)

  • @akki_gamingzone
    @akki_gamingzone 3 ปีที่แล้ว

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

  • @dorlg6655
    @dorlg6655 2 ปีที่แล้ว

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

  • @kevindebruyne17
    @kevindebruyne17 3 ปีที่แล้ว

    Amazing explanation, keep it up!

  • @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

  • @sharkk2979
    @sharkk2979 3 ปีที่แล้ว

    Why u shave, the way u shave?

  • @jeffmason613
    @jeffmason613 5 ปีที่แล้ว

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

  • @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

  • @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

  • @suriveer
    @suriveer 3 ปีที่แล้ว

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

  • @MohammedJavid-jz8vi
    @MohammedJavid-jz8vi 5 ปีที่แล้ว +2

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

  • @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

  • @nosoyyotampoco
    @nosoyyotampoco 4 ปีที่แล้ว

    Great explanation!!!

  • @2awesome4u2000
    @2awesome4u2000 3 ปีที่แล้ว

    I got asked this question in Jan 2021

  • @alanchavez16
    @alanchavez16 5 ปีที่แล้ว

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

  • @sreejith5966
    @sreejith5966 3 ปีที่แล้ว

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

  • @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!

  • @aj9706
    @aj9706 4 ปีที่แล้ว

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

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

  • @vk1618
    @vk1618 4 ปีที่แล้ว

    Start woth a sanity check

  • @cnaught0802
    @cnaught0802 5 ปีที่แล้ว +1

    So helpful!!

  • @Zen-lf7zr
    @Zen-lf7zr 5 ปีที่แล้ว

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

  • @ishanrtripathi
    @ishanrtripathi 4 ปีที่แล้ว

    Thanks

  • @ChandrashekharMuradnar
    @ChandrashekharMuradnar 4 ปีที่แล้ว

    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;

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

  • @ashokbalakrishnan1371
    @ashokbalakrishnan1371 4 ปีที่แล้ว

    You are really veryy cool!!!!

  • @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