# Shortest Path in Binary Trees | CodeChef

I have no idea why this code is not working for the BINTREE easy problem. What i have done in the following code is that i have converted each label into binary and then checked when going from left to right of the binary codes the characters in the elements are same,this would correspond to the common path from root to the common ancestor of the i,j labelled node. then i have calculated the shortest path according to the formula h= k+l-2-2*ctr where k is the number of digits in thebinary code of i and l is the number of digits in the binary code of j. ctr is the number of digits same from left to right excluding the first digit which is always going to the same.
In a nutshell every time we go left we append a 0 to the binary code and every time we go right we append 1 to the binary code of the parent to get the child.

#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int N;long long int i,j,m,k,l,h;deque ch1,ch2;long long int x;long long int ctr;
cin>>N;
for(long long int g=0;g<N;g++)
{
cin>>i>>j;
k=0;
l=0;
while(j!=0)
{
m=j/2;
ch1.push_front(j-2*m);
j=j/2;

``````}
m=0;
while(i!=0)
{
m=i/2;
ch2.push_front(i-2*m);
i=i/2;

}
x=0;ctr=-1;
while(ch1[x]==ch2[x])
{
ctr++;
x++;
}
l=ch1.size();
k=ch2.size();
h=abs(k+l-(2*ctr)-2);
cout<<h<<endl;
ch1.clear();
ch2.clear();
}

return 0;
``````

}

Just keep halving the nodes, till they get equal.
Print the count of halving.

Code - go

But is my logic wrong.