Find distance between 2 nodes in a Binary Tree | Love Babbar DSA Sheet | GFG 🔥 | Amazon | Placements

preview_player
Показать описание
#tree #competitiveprogramming #coding #dsa
Hey Guys in this video I have explained with code how we can solve the problem 'Find distance between 2 nodes in a Binary Tree'.

Follow us on Instagram:

Follow us on LinkedIn:

Hope you like it. Comment if you have any doubt

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

you are doing great job! I have become confident in this topic just because of you

harshitavyas
Автор

bro, i was able to solve this question without looking at the video!! it has happened only because of your awesome explanation in many previous videos that you made and that helped me develop this intuition. many thanks

navyugk
Автор

we can simply return max(a, b)+1 in solve function

shikharsrivastava
Автор

First see lca then see this video and I understand clearly.

sudipc
Автор

Excellent explanation... Super excellent Code.

Raj_Tanvar
Автор

bhagwaan ha bhai tu, jo kaam 2 week mein hona tha, doing it in one day.love you bro

sohansingh-bhfc
Автор

Wonderful explanation! Without you, I'd have stayed illiterate forever 😥

parthsalat
Автор

The code fails at 198'th test case in gfg, I also thought of same logic but it's failing there, even matched mine code with yours.

ShashankKumar-hqcm
Автор

bhai per x + y ko return nahi kar sakte kya ???

dsa_tutorial
Автор

Subtraction of 2 is to handle a particular case, i.e.
If both nodes are the same in which we have to find the distance, in that case the return value will be 2, which should be 0, without substracting 2 it will fail.

howtoed
Автор

Thanks a lot bro! Great explanation :)

SreyesSrinivasan
Автор

Then what is the need of returning a+b+1 could have directly returned a+b and not subtracted 2 later 🙄

abhinavshukla
Автор

Ese medium questions me agar 30-40 min lg rhe he to woh theek he na mtlb? ya thoda or jldi hona chahiye?

veerpratapsinghrathore
Автор

solve(root, target) is nothing but the no. of nodes between root and target, hence no. of edges is nodes -1

rishabhkumar
Автор

Same Logic, but simpler code..

Node *LCA(Node *root, int a, int b){
if(root == NULL) return NULL;
if(root->data == a || root->data==b) return root;

Node *lt = LCA(root->left, a, b);
Node *rt = LCA(root->right, a, b);

if(!lt) return rt;
if(!rt) return lt;
return root;
}

int dis(Node *root, int val){
if(root == NULL) return 1e8;
if(root->data == val) return 0;
return min(1 + dis(root->left, val), 1 + dis(root->right, val));
}

public:
int findDist(Node* root, int a, int b) {
Node *lca = LCA(root, a, b);
int disA = dis(lca, a);
int disB = dis(lca, b);
return disA + disB;
}

KunalKumar-tpcc
Автор

But why +1 when node is found ..any reason ?

ddhimanOfficial
Автор

I am writing the same code but not getting the expected output

snehagoel
Автор

If we don't subtract 2 from final answer & return 0 for *if(root->data==val)*, then we dont get ri8 answer.

Why?

sayantaniguha
Автор

Iterative traversal of tree remaining brother. in, pre and post

pranshuparashar
Автор

im fed up of searching n ary tree topics and getting your binary tree videos lol

jinkem
welcome to shbcf.ru