# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return int整型 # class Solution: def maxDepth(self , root ): left_depth = 0 right_depth = 0 depth = 0 if root is None: return depth if root.right is not None: right_depth += self.maxDepth(root.right) if root.left is not None: left_depth += self.maxDepth(root.left) if root.left is None and root.right is None: return 1 if left_depth > right_depth: depth += left_depth else: depth += right_depth return depth + 1
求给定二叉树的最大深度
此题是典型的深度优先搜索题,我是用python3具体实现