-
Notifications
You must be signed in to change notification settings - Fork 523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1483-kth-ancestor fails 5 out of 15 test cases #18
Comments
In the video Errichto misread 0<=parent[i]<n as 0<=parent[i]<i , therefore in his code he assumed that all the parents are smaller than that node. But that is not the case and it can be corrected by changing the order of the for loop as he had mentioned in the video before, calculating the ith ancestor of every node and then moving forward to calculate the (i+1)th ancestor for every node.
This is a similar code with the only change in the order of for loop. As of now this code passes all the test cases in LeetCode. I have calculated the depths only over 2 iterations but I think in worst case this should have been n-1 times, but that leads to time limit exceeded, that is why I have done it only 2 times. One more approach that I found in the comment section of the youtube video is to make another node n after the last node n-1 ( as indexing is from 0 to n-1) and make the parent[0] = n and parent[n] = n. And in getKthAncestor if the node value equals n that means that ancestor does not exist and that we should return -1. This is the implementation of it.
|
The first approach gives wa on
doing iterations 5 time to find the tree depth does the job . Although I think this solution can be hacked also . |
The provided code doesn't pass all the test cases:
Example:
["TreeAncestor","getKthAncestor","getKthAncestor","getKthAncestor"] [[4,[-1,2,3,0]],[2,3],[2,2],[2,1]]
The text was updated successfully, but these errors were encountered: