1. 程式人生 > >Python, LeetCode, 104. 二叉樹的最大深度

Python, LeetCode, 104. 二叉樹的最大深度

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


class Solution:
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root == None:
            return 0
        depth_l, depth_r = 0, 0
        if root.left:
            depth_l = self.maxDepth(root.left)
        if root.right:
            depth_r = self.maxDepth(root.right)
        return max(depth_l, depth_r) + 1