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

  • 178

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

  • -24
    int maxDepth(TreeNode* root) {
        if (root == NULL)
            return 0;
        stack<TreeNode*> myStack;
        stack<int> depthStack;
        if (root != NULL) myStack.push(root);
        int maxDepth = 0;
        while (!myStack.empty()) {
            TreeNode *p =;
            int d =;
            if (d > maxDepth) maxDepth = d;
            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;

  • 38

    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.

  • 1

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

  • 1
    This post is deleted!

  • 5
    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;

  • 2

    @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.

  • 0

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

  • 0

    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.

  • 0

    public class Solution {
    public int maxDepth(TreeNode root) {
    int left=1;int right=1;
    if (root==null) return 0;

        return Math.max(left,right);


  • 1

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

  • 0

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

  • 0

    @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; 

  • 0

    yes!!! agree!!!

  • 0

    @jdrogin Agree!!!

Log in to reply

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