We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hello. Please can anyone help me with this problem? I'm getting a timeout for the testcase with 100000 queries. I suspect the problem is from my algorithm to calculate the sum. The code is below:
intsumPath(Node*x,Node*y){intsum=0;// while x is on a level below y, ascend while adding the values to sumwhile(x->level>y->level){sum+=x->data;x=x->parent;}// while y is on a level below x, ascend while adding the values to sumwhile(y->level>x->level){sum+=y->data;y=y->parent;}//x and y are on the same level now.//ascend while their parents are not equal and add values to sumwhile(x!=y){sum+=y->data;sum+=x->data;x=x->parent;y=y->parent;}//x and y must be pointing to the same node now which is the lowest common anscestor.//Add the node's value to sum.returnsum+x->data;}
Is there a better way to implement it?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lazy White Falcon
You are viewing a single comment's thread. Return to all comments →
Hello. Please can anyone help me with this problem? I'm getting a timeout for the testcase with 100000 queries. I suspect the problem is from my algorithm to calculate the sum. The code is below:
Is there a better way to implement it?