[META] How is the runtime and memory usage calculated?

I have written the following recursive algorithm for the question in this link:
https://leetcode.com/problems/binary-tree-right-side-view/

Interestingly, submitting the exact same solution for several times gives different runtime and memory usage results:

First submission:

  • Runtime: 28 ms, faster than 92.98%
  • Memory Usage: 13.8 MB, less than 77.79%

Second submission:

  • Runtime: 36ms, faster than 59.95%
  • Memory Usage: 13.9 MB, less than 39.13%

Since the results vary, I thought that the test cases are randomly generated at each submission. But is that the case? I am pretty new around here, so it would be great to learn more on how the runtime and memory usage of each submission is calculated.

class Solution:
    def rightSideView(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        if not root.left and not root.right:
            return [root.val]
        elif not root.left:
            return [root.val] + self.rightSideView(root.right)
        elif not root.right:
            return [root.val] + self.rightSideView(root.left)
        else:
            right = self.rightSideView(root.right)
            left = self.rightSideView(root.left)
            return [root.val] + right + left[len(right):]
			
Comments (0)