Method Explained-
Lets consider-
N-55
OBSERVE THE TREE AND THEN SEE THE SOLUTION
1
11 10
111 110 101 100
we will be checking Breadth First (so think what data structure we will use)
i.e queue
string ans=1;(STORE IN STRING AS ANSWER CAN BE VERY LARGE)
starting with this as number with 1 and 0 cannot start with 0
step-1)pop form q and check
if this no is divisible by N,then this is the answer
step-2)now we have to options
option1-add 1 to string=11
option2-add 0 in string-10
add them to q and repeat step 1
pop 11 and check
11 not divisilbe 2 options
110 and 111
pop 10 amd check
10 not divisible 2 options
100 and 101
then checking we get answer as 110