This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Definition of TreeNode: | |
class TreeNode: | |
def __init__(self, val): | |
self.val = val | |
self.left, self.right = None, None | |
""" | |
# Approach #1 - DFS - T O(n), S O(h), with n being #OfNodes, h being the depth. | |
class Solution: | |
""" | |
@param root: The root of binary tree. | |
@return: An integer | |
""" | |
def maxDepth(self, root): | |
# write your code here | |
if root is None: | |
return 0 | |
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 | |
No comments:
Post a Comment