Print Root to Leaf Path with Given sum(Print all K-Sum paths) in a given Binary Tree

preview_player
Показать описание
Print all the paths with given sum in a binary tree.
Print all K-sum paths in the given Binary tree.
We are using stack in this algorithm.

Рекомендации по теме
Комментарии
Автор

Its pre-order traversal as the root node is being processed first.

agarwalminal
Автор

Good explaination.
But you should also check whether you are at leaf node or not when sum is equal to k.
if(sum==k && p->left==NULL && p->right==NULL)
then print the stack.

Rohankumar-whuu
Автор

Sir, Dil se bol rhaa hoon, best explanation. you have so much of knowledge

naveendubey
Автор

this is not a Root To Leaf PATH SUM Program... this will generate paths in the tree with a given sum not necessarily root to leaf

NachiketJoshiTheBloodMage
Автор

Excellent explanation sir... Tq so much sir....

satyaba
Автор

good explaination sir, i understood your logic well but it's preorder traversal

hemanthn
Автор

interesting to see that you correct your code at 07:46 on the fly ! good job!!!
And it is modified to use Pre-Order traversal of the tree.

jamesqiu
Автор

the traversal that you are doing is definitely preorder, not inorder

TheGreatOne
Автор

looking forward more videos like this, ignore negative comments please, great work👌

ramakrishnakcr
Автор

May be sum needs to be part of recursive call so that while unwinding we will have previous sum without current node ....

vitthalkadam
Автор

This code is not complete. We also need to check if the node is a leaf node, while checking the sum. Thank you for the wonderful explaination.

souravroy
Автор

I think you have to pass the stack reference in the function arguments or else you lose the state of the stack at each recursive function call.

ramakantasamal
Автор

kindly explain complexity in each questions .

nidhisingh
Автор

The paths which don't include roots are not included I think. Please correct me if I am wrong (for example 14), I just saw that it was root to leaf paths, sorry, but could you also provide a code for my problem which doesn't necessarily include root.

spamspam
Автор

Could you please upload a video, how to return randomNode from a binary search tree?

shineychandan
Автор

If there are multiple paths, the first one we get will pop all stack contents while printing. How do restore the data in the stack for printing other paths?

aneeshbose
Автор

Can you show the iterrativ version of this function? Respects.

alperklc
Автор

//by using this no need to take care of poping and decrement sum


void getPath(BTnode* root, stack<int> stk, int sum, int temp)
{
if(root==NULL)
return;

stk.push(root->data);
temp = temp+root->data;

if(root->left ==NULL and root->right ==NULL)
{
if(temp==sum)
{
print(stk);
}
}

getPath(root->left, stk, sum, temp);
getPath(root->right, stk, sum, temp);

}

coder_who_cooks
Автор

is this algo also valid for binary search tree ?

shubhamjindal
Автор

Sir what if root node is not there in the required path, i.e. there is a path in left or right sub tree but does not include root?
Please reply.Thank u in advance

anjana