Don't bother solving in JS, it give a penalty for Time limit exceeded


#1

I did both a recursive and iterative solutions, and in both of them time limit was exceeded, although they’re both running clearly in O(n) time.
When comparing to the editorial solutions in Python - the solutions there (recursive) were exactly the same.
Don’t write it in JS, it’s very short anyway…


#2

Try the following simple and precise recursive code :slight_smile:

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if not root: return 0
        return 1+max(self.maxDepth(root.left) , self.maxDepth(root.right))

#3

module.exports = {
//param A : root node of tree
//return an integer
maxDepth : function(A){
if (A === null){
return 0
}
return Math.max(this.maxDepth(A.left), this.maxDepth(A.right)) + 1
}
};