Spiral (zig-zag) level order traversal of a binary tree

  Рет қаралды 34,281

Vivekanand Khyade - Algorithm Every Day

Vivekanand Khyade - Algorithm Every Day

7 жыл бұрын

Write a program to traverse the binary tree in spiral / zigzag level order traversal.

Пікірлер: 78
@yogeshsharma5168
@yogeshsharma5168 3 жыл бұрын
This man is god for programmer who are struggling to understand programming, he made everything so easy🔥
@girishkumar5764
@girishkumar5764 2 жыл бұрын
sir u r the best teacher, u way of teaching made every thing easy
@sidharthpanda8557
@sidharthpanda8557 4 жыл бұрын
I have been seeing numerous algo and ds videos. This is the best among them.
@lakshay510
@lakshay510 5 жыл бұрын
ITS THE BEST EXPLANATION OUT THERE.
@ranjanrohit64
@ranjanrohit64 5 жыл бұрын
You are the one who makes algorithm so easy to understand.. Lucky to watch your videos.. ❤️
@Deepak-gj4ni
@Deepak-gj4ni 3 жыл бұрын
Sir your content is must for every computer science student
@vibhugarg2248
@vibhugarg2248 7 жыл бұрын
Sir ,you are absolutely the person who is teaching such questions so nicely!Thankyou so much for the help !
@PhilGonzalez
@PhilGonzalez 6 жыл бұрын
Great job, thank you for this explanation!
@yifeiren8632
@yifeiren8632 4 жыл бұрын
Great explanation! Thank you so much!
@siddhu8980
@siddhu8980 6 жыл бұрын
Simple and Amazing explanation.👍🏻👍🏻👍🏻
@bhatanand
@bhatanand 2 жыл бұрын
Brilliant logic. Thanks for the video.
@nemoahuja3423
@nemoahuja3423 2 жыл бұрын
Amazing explanation. Thank you so much!
@reyou7
@reyou7 5 жыл бұрын
I love the he says POPPED 😍 You're great sir!
@amanueltassew804
@amanueltassew804 5 жыл бұрын
lol
@namanvijay3514
@namanvijay3514 3 жыл бұрын
Great Explanation, kudos!
@094_ramankumar9
@094_ramankumar9 3 жыл бұрын
Nice explanation very helpful
@lakshayhurria7703
@lakshayhurria7703 4 жыл бұрын
Nice explanation liked the way you have drawn diagrams to explain
@prashanttiwari120
@prashanttiwari120 5 жыл бұрын
You are doing great job sir
@ruchirai5775
@ruchirai5775 4 жыл бұрын
awesome explanation !
@asdbey735
@asdbey735 3 жыл бұрын
You're a lifesaver bro !! Do more of leetcode, InterviewBit and HackerRank solutions.
@dpfit
@dpfit 4 жыл бұрын
Hi Vivek. Thanks for the nice explanation.. It was very helpful.
@tejaswinysingh8645
@tejaswinysingh8645 3 жыл бұрын
amazing explanation!
@akashbhadouria6727
@akashbhadouria6727 4 жыл бұрын
Thank you🙏💕 sir and happy new year.
@kabboghosh1853
@kabboghosh1853 5 жыл бұрын
best explanation on internet
@sheldon0214
@sheldon0214 2 жыл бұрын
Nice work!
@jaytarwe2774
@jaytarwe2774 7 жыл бұрын
very well explained sir.
@kamalsinghbisht1964
@kamalsinghbisht1964 3 жыл бұрын
Great Explanation Sir
@amitkumargupta6722
@amitkumargupta6722 3 жыл бұрын
just amazing...thanks a lot sir..
@SpiritualManish
@SpiritualManish 7 жыл бұрын
Nice Sir , Thank You :)
@ranjanrohit64
@ranjanrohit64 5 жыл бұрын
Very well expalained sir.
@bishwajeet_b_das
@bishwajeet_b_das 5 жыл бұрын
awesome teaching skill.
@ujjvalsharma5055
@ujjvalsharma5055 4 жыл бұрын
Hi Vivek your videos are amazing and you are a great teacher. Do you have online classes too or maybe be one-to-one doubt session on specific topics?
@pymondo1147
@pymondo1147 5 жыл бұрын
Thank you sir:)
@robertcraig2662
@robertcraig2662 Жыл бұрын
Good explanation. Thnc
@jagadeeshp2558
@jagadeeshp2558 3 жыл бұрын
Amma baboi thopu explanation 🔥🔥
@pranaychandra8016
@pranaychandra8016 5 жыл бұрын
Great Explanation sir please solve more questions like this if you have any online course i am ready to buy it.
@akshaysharma8492
@akshaysharma8492 3 жыл бұрын
awesome sir
@sameerchaudhari8550
@sameerchaudhari8550 6 жыл бұрын
nice explanations long way to go !
@veerrajuyeleti8541
@veerrajuyeleti8541 7 жыл бұрын
good explanation
@vm1662
@vm1662 4 жыл бұрын
Great video! Thanks a lot. Quick question. Is it possible to solve this in constant time?
@rahulkhandelwal46578
@rahulkhandelwal46578 3 жыл бұрын
Thank you
@premprakash8220
@premprakash8220 6 жыл бұрын
nice explanations
@sudiptakumardas3547
@sudiptakumardas3547 4 жыл бұрын
Best vedio
@ruchirsingh3635
@ruchirsingh3635 7 жыл бұрын
nice sir ji
@akashninave5001
@akashninave5001 3 жыл бұрын
Best explanation
@akashninave5001
@akashninave5001 3 жыл бұрын
SIR :)
@subhashishpanda1069
@subhashishpanda1069 3 жыл бұрын
Great!
@kausachan4167
@kausachan4167 3 жыл бұрын
clean
@arjunsabu4927
@arjunsabu4927 4 жыл бұрын
Thank u sir
@ranjeetranjan8753
@ranjeetranjan8753 4 жыл бұрын
thanx sir
@ajayrajrana4376
@ajayrajrana4376 5 жыл бұрын
The best
@travelogue4566
@travelogue4566 7 жыл бұрын
nice explanation sir
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Chandan.!
@keshavmaheshwari521
@keshavmaheshwari521 3 жыл бұрын
mast sir
@veerrajuyeleti8541
@veerrajuyeleti8541 7 жыл бұрын
sir could you do a video on given a binary tree find the largest subtree having atleast 2 other duplicate subtrees
@kaasiimaginarium
@kaasiimaginarium 6 жыл бұрын
Nice
@vikasvk9174
@vikasvk9174 4 жыл бұрын
helpful .... :)
@hrmeet0509
@hrmeet0509 3 жыл бұрын
Its giving segmentation fault in GFG ! please update sir
@subhampandey7374
@subhampandey7374 5 жыл бұрын
What is the time complexity ? O(L) or O(N)
@NATANSHROCKS
@NATANSHROCKS 5 жыл бұрын
O(n)
@jonaskhanwald566
@jonaskhanwald566 3 жыл бұрын
The diagram itself is self explanatory
@VikasGupta2014
@VikasGupta2014 4 жыл бұрын
You should start with recursive solution of the problem.
@rajanrocky7215
@rajanrocky7215 5 жыл бұрын
Code for the spiral model import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.*; //class representing Structure of treeNode in the binary tree class treeNode { int data; treeNode left; treeNode right; treeNode(int value) { data = value; left = null; right = null; } } class Source { static treeNode rootNode; void printSpiral(treeNode rootNode) { if (rootNode == null) return; // NULL check /*Create two stacks named "left2Right" used for printing the levels from left to right and "right2Lef" used for printing the levels from right to left.*/ Stack left2Right = new Stack(); Stack right2Left = new Stack(); // Push root node to the stack right2Left.push(rootNode); // printing the spiral order traversal of the tree while (!right2Left.empty() || !left2Right.empty()) { // print all the nodes in the level from left to right while (!right2Left.empty()) { treeNode tempNode = right2Left.peek(); right2Left.pop(); System.out.print(tempNode.data + " "); // push the right child and then push the left child to the stack "left2Right" if (tempNode.right != null) left2Right.push(tempNode.right); if (tempNode.left != null) left2Right.push(tempNode.left); } // Print all the nodes in the level from right to left while (!left2Right.empty()) { treeNode tempNode = left2Right.peek(); left2Right.pop(); System.out.print(tempNode.data + " "); // push the left child and then push the right child to the stack "right2Left" if (tempNode.left != null) right2Left.push(tempNode.left); if (tempNode.right != null) right2Left.push(tempNode.right); } } } public static void main(String[] args) { Source binaryTree = new Source(); //root node of the binary tree treeNode rootNode; Scanner in = new Scanner(System.in); //number of elements int n = in.nextInt(), element; //queue used to create a binary tree Queue q = new LinkedList(); // creating a new binary tree. rootNode = new treeNode(in.nextInt()); q.add(rootNode); treeNode cur = null; for (int i = 1; i < n; i++) { cur = q.remove(); //Note: if the element is -1 then the node is null element = in.nextInt(); if (element != -1) { cur.left = new treeNode(element); q.add(cur.left); } i++; //Note: if the element is -1 then the node is null element = in.nextInt(); if (element != -1) { cur.right = new treeNode(element); q.add(cur.right); } } //print the spiral order traversal of the tree binaryTree.printSpiral(rootNode); } }
@pulkitkaushik4539
@pulkitkaushik4539 4 жыл бұрын
thanks bete
@TotluTV
@TotluTV 4 жыл бұрын
how this is different from BFS?
@sandeepvedavyas8701
@sandeepvedavyas8701 4 жыл бұрын
In bfs it would not be zigzag. It would print nodes from left to right in all levels unlike this problem where you have to print left to right and right to left for alternating levels.
@SEVERANCE850
@SEVERANCE850 6 жыл бұрын
Won't the space complexity increase due to this
@prashanthgatram237
@prashanthgatram237 3 жыл бұрын
No its the standard one so dont worry
@siddhu8980
@siddhu8980 6 жыл бұрын
Sir can u solve same problem in O(n)
@h3ct0rgr
@h3ct0rgr 5 жыл бұрын
It is O(n) time, all nodes will be popped once from either stack. It is saving two child nodes for each so it is still in order of N.
@dsk9258
@dsk9258 7 жыл бұрын
sir can u please explain the full running code for rubik's cube ... i need it.
@uditkumar867
@uditkumar867 4 жыл бұрын
spiral and zig-zag traversal are not same.
@amitkumargupta6722
@amitkumargupta6722 3 жыл бұрын
vector findSpiral(Node *root) { vector arr; stack s1,s2; s1.push(root); while(s1.empty()==false || s2.empty()==false) { while(s1.empty()==false) { Node *curr=s1.top(); s1.pop(); arr.push_back(curr->data); if(curr->right!=NULL) s2.push(curr->right); if(curr->left!=NULL) s2.push(curr->left); } while(s2.empty()==false) { Node *curr=s2.top(); s2.pop(); arr.push_back(curr->data); if(curr->left!=NULL) s1.push(curr->left); if(curr->right!=NULL) s1.push(curr->right); } } return arr; }
@chetan8524
@chetan8524 2 жыл бұрын
Only one test case passed at gfg
@karthikJampani
@karthikJampani Жыл бұрын
Rather than explaining the steps it would be better to say why we are doing it
@pranaychandra8016
@pranaychandra8016 5 жыл бұрын
If anyone is submitting in geeksforgeeks then this is the vALID SOLUTION FOR IT. ide.codingblocks.com/s/97585
@seraj_valley
@seraj_valley 4 жыл бұрын
don't use it guyes,its giving TLE
@preetirautela3537
@preetirautela3537 3 жыл бұрын
Thank you
Nodes having 'K' leaves in its SubTree Algorithm
14:22
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 7 М.
Bottom view of a Binary Tree Algorithm
12:28
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 36 М.
1,000 Diamonds! (Funny Minecraft Animation) #shorts #cartoon
00:31
toonz CRAFT
Рет қаралды 40 МЛН
Идеально повторил? Хотите вторую часть?
00:13
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 8 МЛН
Print Root to Leaf Path with Given sum(Print all K-Sum paths) in a given Binary Tree
14:56
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 30 М.
L19. Zig-Zag or Spiral Traversal in Binary Tree | C++ | Java
8:21
take U forward
Рет қаралды 230 М.
Level Order Traversal of a Binary Tree (level by level and as a whole)
13:54
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 50 М.
Why The Windows Phone Failed
24:08
Apple Explained
Рет қаралды 234 М.
Boundary traversal of binary tree (Border Elements)
18:20
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 26 М.
This video will destroy your FOMO in 10 minutes.
10:00
What is a Monad? - Computerphile
21:50
Computerphile
Рет қаралды 598 М.
Tree Traversal Spiral Order
17:46
Tushar Roy - Coding Made Simple
Рет қаралды 43 М.
Check if a tree is bst or not
7:15
Techdose
Рет қаралды 34 М.
1,000 Diamonds! (Funny Minecraft Animation) #shorts #cartoon
00:31
toonz CRAFT
Рет қаралды 40 МЛН