maximum depth of binary tree

Maximum Depth of Binary Tree

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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],

return its depth = 3.

translation

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Definition for a binary tree node.
class (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 root is None:
return 0
else:
ldepth = 1+ self.maxDepth(root.left)
rdepth = 1+ self.maxDepth(root.right)
return max(ldepth, rdepth)