Level Order Traversal of a Binary Tree (level by level and as a whole)

Поділитися
Вставка
  • Опубліковано 19 вер 2024

КОМЕНТАРІ • 91

  • @suchitrakunnath232
    @suchitrakunnath232 6 років тому +2

    Program using above algorithm:
    public static void LevelOrderTrversal(TreeNode root)
    {
    Queue q = new LinkedList();
    q.add(root);
    q.add(null);
    int level = 1;
    while(!q.isEmpty())
    {
    TreeNode temp = q.remove();
    if(temp != null)
    {
    System.out.println("Level = " + level + " value = " +temp.val);
    if(temp.left != null) q.add(temp.left);
    if(temp.right != null) q.add(temp.right);
    }
    else
    {
    if(q.peek() == null) break;
    q.add(null);
    level++;
    }
    }
    }

  • @xiangwingrace8672
    @xiangwingrace8672 6 років тому +12

    Amazing great algorithms
    !!!Thank you for posting this. Awesome video as always. Keep up the good work!!

  • @pramodsable4482
    @pramodsable4482 5 років тому +1

    Awesome Teaching Sir... You made algorithms subject easy to understand. I will be always grateful to you.

  • @columbiars
    @columbiars 7 років тому +6

    Excellent explanation Vivekanand!

  • @lresleland4238
    @lresleland4238 5 років тому +1

    thank you for step by step instructions. Makes a big difference in understanding until the end!

  • @ayushvats1808
    @ayushvats1808 3 роки тому

    🔥🔥 the best part is u actually explained so calmly like one by one.
    and explained how the output is actually coming with that algorithm for all cases
    if anything will be explained like this.. concepts gonna so easy

  • @programming_and_snacks5192
    @programming_and_snacks5192 4 роки тому +1

    Thank you sooo much man! No matter what anyone says you are awesome and the way you make things clear is amazing. Thanks again :)

  • @sejalrathore7080
    @sejalrathore7080 2 роки тому +1

    Amazing explanation sir.
    Really liked it👍👍🥳🥳

  • @juhimittal3320
    @juhimittal3320 2 роки тому

    love the way you are explaining the things, best ds tutorials

  • @kirandhedhi2612
    @kirandhedhi2612 7 років тому +4

    hats off keep it up and upload videos in same manner!!
    thanks a lot!!

    • @kirandhedhi2612
      @kirandhedhi2612 7 років тому

      sir where is the code?

    • @Ankit-hs9nb
      @Ankit-hs9nb 6 років тому

      github.com/mission-peace/interview/blob/master/src/com/interview/tree/LevelOrderTraversal.java#L19

  • @vikashkumarchaurasia1299
    @vikashkumarchaurasia1299 6 років тому +1

    Outstanding explanation

  • @m4riders
    @m4riders 2 роки тому

    Very well explained sir

  • @coderix1404
    @coderix1404 3 роки тому

    Awesome explanation

  • @smn656
    @smn656 6 років тому

    Thanks for the great explanation Vivekanand.

  • @kasyapdharanikota8570
    @kasyapdharanikota8570 3 роки тому

    very well explained. Thank you sir .

  • @dimpleshah3037
    @dimpleshah3037 3 роки тому

    Superb explanation

  • @abhisheksaxena6413
    @abhisheksaxena6413 2 роки тому

    great explaination ... i was struggling to understand this

  • @xCwieCHRISx
    @xCwieCHRISx 4 роки тому

    Now I did understand the algorithm on wikipedia. Thank you.

  • @omrajpurkar
    @omrajpurkar 4 роки тому

    Nice Explanation!!
    Thank you 🙏

  • @sweekriteesingh518
    @sweekriteesingh518 4 роки тому

    Very nicely explained 👍

  • @VirendraSingh-hd9yr
    @VirendraSingh-hd9yr 3 роки тому

    thankyou sir for awesome explation

  • @himansuranjanmallick16
    @himansuranjanmallick16 4 роки тому +1

    Awsome logic bro👌

  • @sandeepmunavalli6345
    @sandeepmunavalli6345 7 років тому

    Very Nice and clear explanation sir :)

  • @malkitsaggu
    @malkitsaggu 4 роки тому

    Great explanation.

  • @AvinashKumar-uz9fq
    @AvinashKumar-uz9fq 5 років тому +1

    what will be the looping condition in line by line? If it's loop till queue is not empty then in the if condition p ==null, n the enqueue statement under it will make an infinite loop.

  • @dan-nm2tg
    @dan-nm2tg 6 років тому

    very clear to understand .thanks

  • @ryan-bo2xi
    @ryan-bo2xi 5 років тому

    You are awesome brother. Better than G* i should say !

  • @kkbh4263
    @kkbh4263 5 років тому

    Clear and crisp explanation. But do it with code.

  • @preetamvarun9219
    @preetamvarun9219 4 роки тому

    Man you're awesome seriously

  • @tapanjeetroy8266
    @tapanjeetroy8266 5 років тому +1

    Thank you sir

  • @biplabsarkar4827
    @biplabsarkar4827 7 років тому

    great explanation sir

  • @ashishg656
    @ashishg656 6 років тому

    great video sir, thanks a lot

  • @vivektyson
    @vivektyson 4 роки тому

    Awesome Sir

  • @nitinsridhar7555
    @nitinsridhar7555 4 роки тому

    Who is your guru? Great explaination...!

  • @prasannachalgeri7152
    @prasannachalgeri7152 6 років тому

    Awesome Videos!! I still think there are many missing like finding max element in binary tree using recursion and without recursion which indeed uses LOT. Can you also try to make videos on those topics

  • @shanikumar2019
    @shanikumar2019 5 років тому

    You are awesome,Dear

  • @sachinsingh-jg9lx
    @sachinsingh-jg9lx 4 роки тому

    Hats Off Buddy....

  • @taranjitsingh1115
    @taranjitsingh1115 6 років тому

    great work, man! cheers!

  • @srilekha9177
    @srilekha9177 6 років тому

    Excellent explanation. Mention the codes also. It would be useful.

  • @surajne2419
    @surajne2419 4 роки тому

    Good job Bro. Keep it up :)

  • @amd7407
    @amd7407 5 років тому

    Thanks a lot for the video

  • @Developer.786
    @Developer.786 3 роки тому

    thank u

  • @ashishg656
    @ashishg656 6 років тому +1

    do you provide live coaching for interview preparation. ?

  • @HouseJunkieHUN
    @HouseJunkieHUN 5 років тому

    Thank you, Sir!

  • @tyler_ua6593
    @tyler_ua6593 6 років тому +1

    How can I store each level into an ArrayList? Not just printing them all sequential out.

    • @tyler_ua6593
      @tyler_ua6593 6 років тому

      For everybody who is seeking for the same answer. Here is the link:bit.ly/2HmIOm8

    • @paligamy93
      @paligamy93 6 років тому

      What's your return type look like?

  • @arun26cs
    @arun26cs 5 років тому

    thank you much vivek!!!!

  • @sudamkalpage8107
    @sudamkalpage8107 3 роки тому

    thank you!

  • @HebaHabashkpoper
    @HebaHabashkpoper 4 роки тому

    thank you so much

  • @keyurshah5203
    @keyurshah5203 4 роки тому

    its really good

  • @himanshusattavan1335
    @himanshusattavan1335 5 років тому

    wonderful

  • @NaRuCHi-ch
    @NaRuCHi-ch 5 років тому

    thank you so much !!!!

  • @ArunKumar-wy8qv
    @ArunKumar-wy8qv 4 роки тому

    Thankx sir, So level order traversal and BFS is same ?

  • @prakashraj2411
    @prakashraj2411 4 роки тому

    osm. explanation

  • @Bakepichai
    @Bakepichai 7 років тому

    superb.... :)

  • @anandkulkarni2111
    @anandkulkarni2111 6 років тому +1

    There is much better and simple way to print level by level using recursive call collect items into map and then iterate level by level to print them

    • @HumbleMan78
      @HumbleMan78 5 років тому

      In case recursive function call is expensive , this method is suitable. The extra Queue memory is actually O(n) which is very less

  • @jingli2168
    @jingli2168 5 років тому +1

    Hope this help, but there is another simple version.
    # None version
    def horizon_level(root):
    if root is None:
    return
    queue = []
    queue.append(root)
    queue.append(None)
    while len(queue) != 1:
    p = queue.pop(0)
    if p is not None:
    print(p.data)
    if p.left is not None:
    queue.append(p.left)
    if p.right is not None:
    queue.append(p.right)
    else:
    queue.append(None)
    return

  • @ramakrishnakcr4417
    @ramakrishnakcr4417 3 роки тому

    ❤️

  • @i_amsinha
    @i_amsinha 4 роки тому +1

    If the tree node has value of type "int" , the Null solution for line by line traversal isn't gonna work.

    • @talal194
      @talal194 3 роки тому

      You can enqueue -1 then

  • @Vishal-ng2xb
    @Vishal-ng2xb 7 років тому

    Hi ..very good explanation..but for left and right view how do I determine the node?for left view maybe the 1st node after each null..but for right view?

    • @milimishra6447
      @milimishra6447 7 років тому

      for right view it will be the end node at each level...

    • @RANJANJHA95
      @RANJANJHA95 6 років тому

      Using level by level (not in one line)
      For the left view: the first root node and then each node just after null.
      For left view: Any node just before null

  • @DeepakPandey
    @DeepakPandey 7 років тому

    Hi @vivekanand thanks for explanatory videos.
    Could you please cover trie data structures and there applications. Thanks in advance !

  • @Vishal-ng2xb
    @Vishal-ng2xb 7 років тому +1

    How do I check for consecutive null

    • @sunitshrivastava4448
      @sunitshrivastava4448 7 років тому

      inside if loop, check for next element of queue to be null and break out of while loop.

    • @jingli2168
      @jingli2168 5 років тому

      check the length of the queue if len == 1 then stop the while loop, cause only None in the queue

  • @milimishra6447
    @milimishra6447 7 років тому

    nice..

  • @moayyadarz2965
    @moayyadarz2965 4 роки тому

    really it is amazing and outstanding explanation
    If we want to communicate with you how can we do that ?
    we ,a group of students from turkey ,want to take a paid course online from you
    hopefully you could help us
    all the best

  • @paraskumar693
    @paraskumar693 3 роки тому

    This is vey High Lebel thinking

  • @DentrifixoRam88
    @DentrifixoRam88 7 років тому

    great job!

  • @tanzeemahmed9774
    @tanzeemahmed9774 7 років тому

    Great video. Do you have code sample?

    • @Ankit-hs9nb
      @Ankit-hs9nb 6 років тому

      github.com/mission-peace/interview/blob/master/src/com/interview/tree/LevelOrderTraversal.java#L19

  • @tusharsingh6792
    @tusharsingh6792 3 роки тому

    will u please implement the same in code

  • @raj-nq8ke
    @raj-nq8ke 3 роки тому

    (Dequeue Print Enqueue ) untill queue is empty.

  • @noureenislam7691
    @noureenislam7691 6 років тому

    where do i find the code?

  • @VISHALSHARMA-mu9kl
    @VISHALSHARMA-mu9kl 3 роки тому

    sir plzz also provide code for this

  • @ArpitDhamija
    @ArpitDhamija 4 роки тому

    Iska code.....

  • @brahmam9842
    @brahmam9842 5 років тому

    Everything is Good.Improve English Bro.(Just suggestion)

  • @jaysahu357
    @jaysahu357 7 років тому

    thank you sir