Sunday, August 8, 2021

LintCode 97 · Maximum Depth of Binary Tree.py

"""
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