每日一道算法题--leetcode 104--二叉树的最大深度--python

231 阅读1分钟

【题目描述】

【代码思路】

【一、递归实现】 非常简单的深度优先,直接看代码。

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

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

【二、非递归】明日更新