Leetcode 104 Maximum Depth of Binary Tree python

题目:

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

 

 求二叉树的深度,递归一下就可以求到了。

python新手(我)发现python中没有三元运算符,不过可以用true_part if condition else false_part模拟。虽然我后面用max函数代替了三元运算符,也实现了功能。

 

1 class Solution(object):
2     def maxDepth(self, root):
3         if (root == None):
4             return 0
5         left_depth = self.maxDepth(root.left)
6         right_depth = self.maxDepth(root.right)
7         return max(left_depth, right_depth) + 1

 

posted on 2016-04-15 22:42  def_sysu  阅读(295)  评论(0)    收藏  举报

导航