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:
Second submission:
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):]