Binary Tree Right Side View

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • For business inquiries email partnerships@k2.codes Discord: bit.ly/K2-discord
  • Věda a technologie

Komentáře • 90

  • @tannerbarcelos6880
    @tannerbarcelos6880 Před 3 lety +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 Před 4 lety +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 Před 2 lety +1

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

  • @tadiarvindraju2157
    @tadiarvindraju2157 Před 4 lety +2

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

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +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!

  • @psn999100
    @psn999100 Před 5 lety +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  Před 5 lety

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

  • @surajgrandhi6742
    @surajgrandhi6742 Před 4 lety

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

  • @coderbehindthescreen9336

    loving your approach :-)

  • @mayanksharma9827
    @mayanksharma9827 Před 2 lety

    You are seriously one of the best teacher

  • @indranilthakur3605
    @indranilthakur3605 Před 4 lety +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 Před 2 lety

      something like Bottom to Top

    • @AnimeshPaul23
      @AnimeshPaul23 Před 2 lety +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.

  • @Uber_handle
    @Uber_handle Před 2 lety

    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/

  • @jinxscript
    @jinxscript Před 2 lety

    need more of this man 💔

  • @saipavanpurella9253
    @saipavanpurella9253 Před 3 lety

    man couldn't find a teacher better than you

  • @hermanyniu2292
    @hermanyniu2292 Před 4 lety +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?

  • @kevindebruyne17
    @kevindebruyne17 Před 3 lety

    Amazing explanation, keep it up!

  • @AnimeshPaul23
    @AnimeshPaul23 Před 2 lety

    Great explanation.

  • @sumitthakur7526
    @sumitthakur7526 Před 3 lety

    You made me love tree!!!!

  • @dinsummer535
    @dinsummer535 Před 3 lety

    Nice simple explanation :)

  • @satheshbm92
    @satheshbm92 Před 4 lety +1

    Perfect. Thank you Kevin :)

  • @mohammedalmukhtar8949
    @mohammedalmukhtar8949 Před 4 lety +2

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

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +1

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

  • @huizhao2050
    @huizhao2050 Před rokem

    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)

  • @michaeldang8189
    @michaeldang8189 Před 3 lety +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 Před 2 lety

      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

  • @danni6113
    @danni6113 Před 5 lety +4

    Great work!

  • @bauminds6304
    @bauminds6304 Před 5 lety +1

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

  • @paullin7636
    @paullin7636 Před 5 lety +4

    Very nice content thank you so much for the video.

  • @TrollFreeInternet
    @TrollFreeInternet Před 5 lety +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  Před 5 lety +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 Před 3 lety +5

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

  • @dorlg6655
    @dorlg6655 Před 2 lety

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

  • @suriveer
    @suriveer Před 3 lety

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

  • @HardikShah13
    @HardikShah13 Před 4 lety +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 Před 2 lety +3

      left subtree can have depth longer than right subtree

  • @aribasiebel
    @aribasiebel Před 5 lety +3

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

  • @julietgeorge4858
    @julietgeorge4858 Před 4 lety

    God, you are amazing!

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

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

  • @divyanshuchaudhari5416
    @divyanshuchaudhari5416 Před 3 lety +1

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

  • @akki_gamingzone
    @akki_gamingzone Před 3 lety

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

  • @nosoyyotampoco
    @nosoyyotampoco Před 4 lety

    Great explanation!!!

  • @cnaught0802
    @cnaught0802 Před 5 lety +1

    So helpful!!

  • @GauravKumar-by1yt
    @GauravKumar-by1yt Před 5 lety +2

    Thanks Kevin Sir

  • @flavorfabs
    @flavorfabs Před 5 lety +2

    Well done dude

  • @deepakdas4513
    @deepakdas4513 Před 4 lety

    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.

  • @treyquattro
    @treyquattro Před 4 lety

    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!

  • @jeffmason613
    @jeffmason613 Před 5 lety

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

  • @vyshnavig1723
    @vyshnavig1723 Před 3 lety

    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

  • @sharkk2979
    @sharkk2979 Před 3 lety

    Why u shave, the way u shave?

  • @ishanrtripathi
    @ishanrtripathi Před 4 lety

    Thanks

  • @mahmoudmaamoun6422
    @mahmoudmaamoun6422 Před 4 lety

    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 Před 4 lety

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

  • @arno.claude
    @arno.claude Před rokem

    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.

  • @alanchavez16
    @alanchavez16 Před 5 lety

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

  • @Nobody2310
    @Nobody2310 Před 5 lety +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 Před 3 lety +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.

  • @ChandrashekharMuradnar

    thanks

  • @aj9706
    @aj9706 Před 4 lety

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

  • @nileshsharma5377
    @nileshsharma5377 Před 3 lety

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

  • @pankajrathi
    @pankajrathi Před 3 lety

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

  • @sharpnova2
    @sharpnova2 Před 5 lety +5

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

  • @MohammedJavid-jz8vi
    @MohammedJavid-jz8vi Před 5 lety +2

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

  • @lavishgarg4274
    @lavishgarg4274 Před 4 lety

    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 Před 4 lety

      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

  • @2awesome4u2000
    @2awesome4u2000 Před 3 lety

    I got asked this question in Jan 2021

  • @vk1618
    @vk1618 Před 4 lety

    Start woth a sanity check

  • @ashokbalakrishnan1371
    @ashokbalakrishnan1371 Před 4 lety

    You are really veryy cool!!!!

  • @sreejith5966
    @sreejith5966 Před 2 lety

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

  • @MrPlanes747
    @MrPlanes747 Před 5 lety

    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 Před 4 lety +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;

  • @Zen-lf7zr
    @Zen-lf7zr Před 4 lety

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

  • @my3m
    @my3m Před 4 lety

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

  • @rohankulkarni7081
    @rohankulkarni7081 Před 2 lety +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);
    }
    }

  • @surajgrandhi6742
    @surajgrandhi6742 Před 4 lety

    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