# Can leetcode share top performing solution(s) of problems for each supported language ?

• After a solution is accepted it would be very helpful to know how to make it run faster looking at better performing solution(s).

• ``````int maxDepth(TreeNode* root) {
if (root == NULL)
return 0;
stack<TreeNode*> myStack;
stack<int> depthStack;
if (root != NULL) myStack.push(root);
int maxDepth = 0;
depthStack.push(0);
while (!myStack.empty()) {
TreeNode *p = myStack.top();
int d = depthStack.top();
if (d > maxDepth) maxDepth = d;
myStack.pop();
depthStack.pop();
if (p->left != NULL) {myStack.push(p->left); depthStack.push(d + 1);}
if (p->right != NULL) {myStack.push(p->right); depthStack.push(d + 1);}
}
return maxDepth + 1;
}``````

• Sometimes one's solution is of top performing just because the server was at very good form when he's coding. Leetcode's server configurations are probably changing as well, as I found my old solution would have poorer performance in new submissions.

• posted in the wrong place...man, -10. +1 for your confidence.

• This post is deleted!

• ``````int maxDepth(TreeNode* root) {
if(root == NULL){
return 0;
}
int depth_left = maxDepth(root->left) + 1;
int depth_right = maxDepth(root->right) + 1;
return depth_left > depth_right ? depth_left : depth_right;
}``````

• @rainhacker I agree this would be awesome to see (and learn). But the OJ times are very inconsistent, something might score in top 25% on one submission and then submitting the exact same solution a minute later might score in bottom 25%. It's hard to derive much meaning from the OJ times.

I would love to see some actual measurement of the number of cycles or calculations or something not dependent on time or maybe just running your solution 100 times and getting an average may make the number more meaningful. It's not easy to figure out the best way to "measure" a solution quantitatively and in the absence people tend to place a lot of value on the brevity of their solution "3 lines!" which to me doesn't seem to be a good indicator of anything really.

• @jdrogin Agree. Since it is an algorithm website, could we at least have a reliable runtime?

• @jdrogin
On top of this discussion.
I wonder if the run time is the TOTAL of run time for each individual testcase.
If this is the case (and I believe so), we could just randomly generate some big testcases pools.
Especially for algorithm questions that operations on integers / array of integers / etc.

Segmenting the runtime in the bandwidth of 1 ms could be meaning less when you have just a handful of testcases.
I imagine, even with the same time / space complexity, depending on the way people code, the run time could still vary a bit.
And the only way to distinguish would be to have a big pool of testcases.

• public class Solution {
public int maxDepth(TreeNode root) {
int left=1;int right=1;
if (root==null) return 0;
else{
if(root.left!=null){
left=maxDepth(root.left)+1;
}
if(root.right!=null){
right=maxDepth(root.right)+1;
}
}

``````    return Math.max(left,right);
}
``````

}

• come to JAVA and you will find the best answer every time.

• @Patrick1993 I think this answer is better than mine because I called the recursive function in my return value, which doubles my runtime. Thx

• @muzhou @Patrick1993 my solution is very similar to Patrick1993's solution except that I save on one addition by first comparing the values return by recursive call and then only adding to the one that is greatest.

``````    public int maxDepth(TreeNode root) {
if (root == null) return 0;
int maxL = maxDepth(root.left);
int maxR = maxDepth(root.right);
if ( maxL > maxR) return maxL + 1; else return maxR + 1;
}
}
``````

• yes!!! agree!!!

• @jdrogin Agree！！！

• can't agree more.

• If you want to see other submitted solutions based on runtime, you can click the "More Details" button after your solution was accepted. Then, on the chart of runtimes, click on any of the data points to see a solution that was accepted with that runtime.

Hope that helps!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.