Тёмный

Print all ROOT to LEAF paths in a binary tree 

Vivekanand Khyade - Algorithm Every Day
Подписаться 114 тыс.
Просмотров 41 тыс.
50% 1

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 67   
@musa1946
@musa1946 3 года назад
I love this guy the most when it comes to videos. he doesn't just write the code, he draws out all the steps the code actually takes, which makes it much more intuitive.
@vivekanandkhyade
@vivekanandkhyade 3 года назад
Thanks
@BaishaliGhosh13
@BaishaliGhosh13 6 лет назад
Queue is better choice as we are printing FIFO order and stack is LIFO. Great tutorial and good explanation.
@pragya3450
@pragya3450 5 лет назад
haa...how can we print stack in opp direction, way too effort
@jayeshbaviskar
@jayeshbaviskar 4 года назад
But at the time of removing an element, in FIFO root element will be removed first instead of a leaf node. So the queue is also a not good DS to solve this problem. Instead of it, we can use Double-ended queues.
@foo.1396
@foo.1396 6 лет назад
Is it only me who thinks it's actually a mixture of in-order and pre-order traversal? Have a look at 7:10. 1> pushing data to stack 2> recursive call with left child 3> printing stack 4> recursive call with right child
@josiahdavid7735
@josiahdavid7735 2 года назад
I was thinking the same, I believe its actually a pre-order traversal.
@pranaykumar9433
@pranaykumar9433 3 года назад
sir if we print the stack then we have to one by one pop all elements uot of stack which will not work
@accepted5856
@accepted5856 4 года назад
Using queue is more efficient rather than a stack to display a root
@tolanCode
@tolanCode 2 года назад
for the first print, "a,b,d".... how do you print the first path without popping everything from the list. in the next step you only pop d on the top but not everything else
@arnobchowdhury1804
@arnobchowdhury1804 4 года назад
pythonistas no worries: use reversed ()
@vivekanandkhyade
@vivekanandkhyade 4 года назад
hehe....that's true...!!! pythonistas are legends..
@sanketkumbhare2307
@sanketkumbhare2307 7 месяцев назад
Please add code link to your description
@muskanpa
@muskanpa 2 года назад
he is good with theory but code is wrong
@sushma6314
@sushma6314 6 лет назад
Your name is apt for u
@suhasnayak4704
@suhasnayak4704 5 лет назад
Same logic using Deque(Java implementation) public static void rootToLeafPaths(TreeNode root,List al,Deque q){ if(root==null) { return; } q.add(root); rootToLeafPaths(root.left, al, q); if(!q.isEmpty() && root.left==null && root.right==null) { List ls=new ArrayList(); Iterator it=q.iterator(); while(it.hasNext()) { TreeNode temp=(TreeNode) it.next(); ls.add(temp.val); } al.add(new ArrayList(ls)); } rootToLeafPaths(root.right, al, q); q.removeLast(); }
@pruthvirajk6019
@pruthvirajk6019 4 года назад
Wonderfullllllllllllllll
@vinayak219
@vinayak219 7 лет назад
Hello Vivek, Nice efforts from you. I have few queries. Please help me understand. 1. According to code when we reach the leaf node, program will print the path from root to leaf. For that elements should be popped from the stack. For ex: to print "abd", one needs to do pop() 3 times. Now, the stack is empty. Why we need to pop() again ? Does print_stack() is followed in different way. 2. If we use stack to print the path then "abd" will be "dba" when we print Please correct if wrong. Thanks
@junayedmahmud191
@junayedmahmud191 3 года назад
just use list instead of stack listt; void print_path() { for(list::iterator it=t.begin();it!=t.end();it++) { Node *temp=*it; cout
@tempregex8520
@tempregex8520 3 года назад
i believe we can use a DFS based approach on this?no need of extra space? Also, can someon show me a reference to an iterative solution to this? can we think an iterative version will use a stack, and it will be the same exact way we would perform a DFS(with Stack) on a graph, except we won't need a "visited" check?
@sushma6314
@sushma6314 6 лет назад
Thanks for making trees simple
@ShreyaSingh-vr9qi
@ShreyaSingh-vr9qi 4 года назад
Your approach is efficient if we have to print leat to root path but here we have to print root to leaf path so either we can use list or deque to solve this problem, every time reversing the stack ( not by taking reference ) or store it an a container is not an efficient technique.
@atharshg2367
@atharshg2367 7 лет назад
sir ,could u explain,..Given a binary tree, find the maximum path sum?
@vivekanandkhyade
@vivekanandkhyade 7 лет назад
yes sure atharsh ....very soon i will uploadvideo in this week.
@shubhamsunny6024
@shubhamsunny6024 5 лет назад
#include #include using namespace std; struct node{ int data; struct node *left,*right; }; struct node *newnode(struct node *start,int data) { struct node *temp=(struct node *)malloc(sizeof(struct node)); temp->data=data; temp->left=temp->right=NULL; return temp; } void postorder(struct node *start) { if(start==NULL) return; postorder(start->left); postorder(start->right); coutright) q.push(temp->right); } } coutright=newnode(root,7); postorder(root); find_level_maximum_sum(root); return 0; }
@harshvijeta8230
@harshvijeta8230 4 года назад
For those who are doubting how to print stack in reverse order. Instead of using stack you can use vector as it has all those functionality of push_back() and pop_back()
@chenzhuo9
@chenzhuo9 3 года назад
great video! thank you for your help!
@nemanjajocic2816
@nemanjajocic2816 7 лет назад
Hello sir, longestLeftPath(Node* n,Node** start) function should find longest path in binary tree only count if we move left in BST ,also function should return reference of node where longest left path i BST start . Any idea ? Thanks for videos.
@Mohamed-M-M
@Mohamed-M-M 6 лет назад
Thank you, Great, only I would say, it has to be Queue instead of stack, the stack will print in reversed order. thank you, its really great tutorial.
@indavarapuaneesh2871
@indavarapuaneesh2871 6 лет назад
but if we will use stack then ,when we away from leaf node ,removal of top node is difficult.Instead we should use a deque.
@adityarajar8285
@adityarajar8285 5 лет назад
Then how will you pop it.Deque may be a choice but Instead You can implement your own stack using array and print elements starting with index 0 till top of stack.
@yashchandraverma3131
@yashchandraverma3131 4 года назад
It can be easily done if you using list(array) as the stack. See solution- leetcode.com/problems/binary-tree-paths/discuss/531333/Python3-Runtime%3A-20ms-InOrder-Traversal-%2B-Stack
@arnabchakraborty246
@arnabchakraborty246 4 года назад
Thank youuuuu very muchhh sirrrr
@pakhandii
@pakhandii 6 лет назад
apne return type galat liya h shayad it show be a node* type;
@sasafassafsa4155
@sasafassafsa4155 3 года назад
Thank you apnay aik kameenay teacher say jaan bacha li
@studyonline3236
@studyonline3236 5 лет назад
we can also do it using pos-torder traversal
@sujitgupta1583
@sujitgupta1583 7 лет назад
Sir Please upload video on Trie Data structure with Problems also.
@senthilbalaji6489
@senthilbalaji6489 4 года назад
Thank you for wonderful tutorial!
@AddieInGermany
@AddieInGermany 7 лет назад
Sir please mention time and space complexities in every video. You miss it everytime.
@ahmedouyahya
@ahmedouyahya 4 года назад
Thank you soo much
@abhishekraj8566
@abhishekraj8566 5 лет назад
nice explanation
@dimitridoroshko
@dimitridoroshko 6 лет назад
Thank you for your lesson, Sir! It helped me a lot!
@RameshPapagvnti
@RameshPapagvnti 7 лет назад
I think istead of stack, it's better to use queue, so that order would be root to child
@Darshil1771
@Darshil1771 5 лет назад
But while popping out element you need to remove it from the end of the queue. So either case would work fine!
@Debsahab
@Debsahab 7 лет назад
Hi vivekanand.. Thanks for the nice explanation, but how do you print the stack from bottom ?
@chandrashekarpaladugula4731
@chandrashekarpaladugula4731 6 лет назад
I written whole code as follows: int intArray[10240]; int counter = 0; void printAllPaths(Node* root) { if(root == NULL) return; intArray[counter] = root->val; counter++; printAllPaths(root->left); if(root->left == NULL && root->right == NULL) { for(int i=0; i
@veerrajuyeleti8541
@veerrajuyeleti8541 7 лет назад
sir could you do a program for find the sum of the diagonal elements in a binary tree
@vivekanandkhyade
@vivekanandkhyade 7 лет назад
Yes sure very soon Veerraju..!
@nidhishsinghal5167
@nidhishsinghal5167 7 лет назад
while printing stack (print_stack()) we have to pop() out all the elements then how will we get track of last values
@vivekanandkhyade
@vivekanandkhyade 7 лет назад
Yes you are right......but in this case while printing you just go through the stack_array and print that array , don't pop() values . You can always display the current elements in the stack. Just take the snapshot of the stack at that instant. You just have to keep a counter of how many elements are present in the array. Here I have just focussed on the algorithm for root to leaf paths....but if you say I will mail you the code for the same. Hey thanks for helping me to get better.
@koushikahamedkushal
@koushikahamedkushal 7 лет назад
after ending the print stack scope we have to go to the next line to execute pop if(root.left==null && root.right==null){ print stack; } pop stack;
@pranaychandra8016
@pranaychandra8016 5 лет назад
bhai jawab milla to muje bhi btao kese stack ko intact rakha without popping?
@akkiei
@akkiei 6 лет назад
Great idea. Well done.
@sumanghosh3541
@sumanghosh3541 6 лет назад
you are too good
@coolliarsucks5009
@coolliarsucks5009 7 лет назад
Hi, I have a doubt in the way, push(root->data ) should come before we checking if (root ==null) and print should come in if (root ==null ) condition. Correct me if am worng
@jnana2306
@jnana2306 6 лет назад
So what if root is null. It will break the code if you do that before checking (root == null)
@alizaman239
@alizaman239 6 лет назад
Thank you for the clear explanation. However I think printing the stack would print the elements in LIFO order. Which means that you would need to pop from stack and hold the values some where else or some how print in reverse order. Correct?
@rodrickedwards2759
@rodrickedwards2759 5 лет назад
Correct
@edwardkeselman5118
@edwardkeselman5118 7 лет назад
It is also can be done using pre-order: void preorder(Node* root, stack s){ if( root == null ) return; s.push(&root); preorder(root->left,s); preorder(root->right,s); if( root -> left == null && root -> right == null) printStack(s); s.pop(); return; }
@harshvijeta8230
@harshvijeta8230 4 года назад
It is not pre order , it is post order
@edwardkeselman5118
@edwardkeselman5118 3 года назад
You're correct
@INSPIRINGNMS
@INSPIRINGNMS 7 лет назад
Sir ... i think in above video , we are using preorder traversal instead of inorder . please clarify me if i am wrong
@rabindrapatra7151
@rabindrapatra7151 4 года назад
any thing is fine. A + B is in order, +AB is pre order, AB+ is post order. If I say only AB. which order it is. He is just using AB.
@nguyenhuythong9750
@nguyenhuythong9750 6 лет назад
I need a non-recursive algorithm, can you tell me?
Далее
Print number of leaf nodes(leaves) in Binary Tree
6:19
Bottom view of a Binary Tree Algorithm
12:28
Просмотров 36 тыс.
I Took An iPhone 16 From A POSTER! 😱📱 #shorts
00:18
Path Sum III | LeetCode 437 | Medium
25:01
Просмотров 13 тыс.
Diameter of a Binary Tree (Code/ Algorithm)
17:15
Просмотров 94 тыс.