Level by Level Printing of Binary Tree

  Рет қаралды 66,424

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

Күн бұрын

Пікірлер: 49
@sandeepannits86
@sandeepannits86 8 жыл бұрын
It is such a relief brushing up things watching your videos one night before interviews :)
@xVermyy
@xVermyy 5 жыл бұрын
Ayyy I'm in the exact same position lol
@Masa-lo4uw
@Masa-lo4uw 3 жыл бұрын
same here
@rahuldevmishra4558
@rahuldevmishra4558 7 жыл бұрын
At 7:08 - The third way - I think the variable currentCount is not necessary. The currentCount holds the count of elements present in the next level. We can get the same value from queue.size() when the levelCount becomes 0 i.e. once the levelCount becomes 0, the only elements present in the queue would belong to the next level. As you clearly explained, as long as levelCount is not 0, remove the element from the queue and add it's children. Once the levelCount becomes 0, then reset the levelCount to queue.size(). I was able to code this without using the currentCount. And thanks a lot for the video.
@amishashahpatel
@amishashahpatel 4 жыл бұрын
This is really very helpful. This will in fact help solving many problems like Populating Next Right Pointers in Each Node. Thank you so much!
@daspradeep
@daspradeep 6 жыл бұрын
in the first 2 q approach, code can be made simpler by swapping the queues such that you are always operating on the same q
@tusharroy2525
@tusharroy2525 6 жыл бұрын
Yes that's a good idea too.
@dibyajyoti_ghosal
@dibyajyoti_ghosal 4 жыл бұрын
great explanation, it's sad that you don't make videos anymore, maybe apple is not letting you. But your dp contents can be re-uploaded with more explanation on how to approach problems.
@ANANDJULU
@ANANDJULU 6 жыл бұрын
Before addinga null, we need to make sure 1. we get null returned from queue and also 2. queue is not empty Else it will go in an infinite loop.
@CEngineer2010
@CEngineer2010 8 жыл бұрын
Around Time 13:27, you forgot Q1.pop and Q2.pop within your inner two while loops. Thank you for posting. Without popping the values from the queue within the two loops. It will create an infinite loop.
@ankishbansal420
@ankishbansal420 5 жыл бұрын
Thanks Tushar, you are very helpful as always.
@vgrunert
@vgrunert 7 жыл бұрын
Excellent tutorials. Keep up the good work. They really help.
@DarshanWashimkar
@DarshanWashimkar 9 жыл бұрын
Why do you need two queue and so many complications? BFS will print desired output. For example, use one queue, visit node(print node), put left and right child in the queue,...pop node from queue.....repeat the process until your queue is empty.
@debabhishek
@debabhishek 8 жыл бұрын
+Tushar Roy even if you want line by line the regular BFS will work.. and I Q is enough.. let me write sudo code. ( and you dont need a delimeter too). let me me write sudo code put root in Q.[ Q has 1 elment root] while ( Q is not empty) { // outer loop int loop_run=Q.size(); for ( i=0;i<loop_run;i++){ // inner loop current node = Q.enqueue(); if ( current_node.left !=null) Q.addelement( curent_node.left); if ( current_node.right !=null) Q.addelement( curent_node.right); print current_node; } // inner loop ends System.out.println(" " ); // look when inner loop ends you have printed 1 level .. // and other level elelements are in Q.so next for loop will print next level. } // outer loop ends * this is very similar to your third approach. ( I have not seen it before commenting it)
@shubhamvadhera
@shubhamvadhera 8 жыл бұрын
+Tushar Roy It can be done using one queue only. Take the size of queue at every iteration and dequeue until current size.
@abdallaelmedani8933
@abdallaelmedani8933 4 жыл бұрын
Great stuff. Thank you so much for making this video.
@sumitchohan9473
@sumitchohan9473 7 жыл бұрын
It will make more sense if you use word "enqueue" and "dequeue" instead of "push" and "pop" when you work with queues. With this no need to specify if you intend to add/remove them from front/rear.
@shivanimall3820
@shivanimall3820 9 жыл бұрын
Thank you for the videos. They really help. Can you also make one to find the depth of a node in a binary tree?
@shivanimall3820
@shivanimall3820 9 жыл бұрын
+Shivani Mall Or would this approach work? while finding the height of a tree, instead of reaching the leaf we reach the desired node and return from there.
@shivanimall3820
@shivanimall3820 9 жыл бұрын
***** Thank you. That helps!!
@shivanimall3820
@shivanimall3820 9 жыл бұрын
Also, do you have a video on finding a circular loop in a singly linked list? I am having troubles understanding a part of it.
@skariaroy5988
@skariaroy5988 6 жыл бұрын
Why can’t you create an array of the same size as the queue and map each tree element to the array like we do in heap. If there is no element we can put null. Then just print from the array by skipping all Nulls?
@basedmangoes3379
@basedmangoes3379 8 жыл бұрын
Awesome video as always. Keep up the good work man.
@yamanshiekhdeia4778
@yamanshiekhdeia4778 5 жыл бұрын
Dudeeee! you're a national treasure!
@kaivulya
@kaivulya 3 жыл бұрын
Greaaaatttt!
@alfabinomial6183
@alfabinomial6183 5 жыл бұрын
sir i didnt understand that you said the time complexity is O(n) but this function is working with inner loop ..could you please explain !! i think its O(n^2)
@HemaLathadaksnamurthymyfamily
@HemaLathadaksnamurthymyfamily 4 жыл бұрын
Is this same as creating a linked lists of all nodes at each depth for a binary tree??
@annastakanova1115
@annastakanova1115 5 жыл бұрын
thanks so much!
@shobhitranjan3957
@shobhitranjan3957 4 жыл бұрын
coding in python there is no poll() fun what should i do?
@נדבסלמן-ט2מ
@נדבסלמן-ט2מ 8 жыл бұрын
Amazing great algorithms
@SatyendraJaiswalsattu
@SatyendraJaiswalsattu 8 жыл бұрын
great...
@CraftandDogs
@CraftandDogs 5 жыл бұрын
this is smart approach! Thanks \0/
@neelamchahal9299
@neelamchahal9299 5 жыл бұрын
nice t-shirt
@magicodabola10
@magicodabola10 6 жыл бұрын
Thanks for the vid but you are using stacks and not queues. When you say pop and push it's functions of stacks, in queues it's called dequeue and enqueue. Also the "pop" that you are referring to is the LIFO type, which is stacks of course..
@rashmimishra3031
@rashmimishra3031 9 жыл бұрын
Hi Tushar, could you please explain backtracking too.
@rashmimishra3031
@rashmimishra3031 9 жыл бұрын
***** Hi Tushar sorry for confusion. I asked you to explain backtracking in general.
@SahilSharma-ir7ix
@SahilSharma-ir7ix 6 жыл бұрын
@ThexAzox
@ThexAzox 7 жыл бұрын
ya me chantaron el rojo men :,v
@VuongGiia
@VuongGiia 6 жыл бұрын
how about C++??
@vishalmishra1937
@vishalmishra1937 6 жыл бұрын
is it similar to bfs
@xjsnjkil2070
@xjsnjkil2070 6 жыл бұрын
I HIT THE LIKE BUTTON BECAUSE YOU ARE A SEAHAWKS FAN.
@AdityaFingerstyle
@AdityaFingerstyle 7 жыл бұрын
Using a recursive function void display(node* Ptr) { if(Root == NULL) { cout left); cout data right != NULL) display(Ptr->right); } }
@EternalGlory6
@EternalGlory6 5 жыл бұрын
arkadaşlar lütfen aramızda para toplayıp hocama bilgisayar alalım adam kodu tahtaya yazıyor :'(
@MohitK96
@MohitK96 4 жыл бұрын
could take better example
Reverse level order traversal binary tree
6:54
Tushar Roy - Coding Made Simple
Рет қаралды 44 М.
Lowest Common Ancestor Binary Tree
11:08
Tushar Roy - Coding Made Simple
Рет қаралды 252 М.
VAMPIRE DESTROYED GIRL???? 😱
00:56
INO
Рет қаралды 8 МЛН
REAL 3D brush can draw grass Life Hack #shorts #lifehacks
00:42
MrMaximus
Рет қаралды 12 МЛН
Morris Inorder Tree Traversal
11:44
Tushar Roy - Coding Made Simple
Рет қаралды 146 М.
Check if Binary Tree is Binary Search Tree
10:29
Tushar Roy - Coding Made Simple
Рет қаралды 122 М.
A* (A Star) Search Algorithm - Computerphile
14:04
Computerphile
Рет қаралды 1,1 МЛН
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
VAMPIRE DESTROYED GIRL???? 😱
00:56
INO
Рет қаралды 8 МЛН