LeetCode小白菜笔记[19]:Maximum Depth of Binary Tree
LeetCode小白菜笔记[19]:Maximum Depth of Binary Tree
104. Maximum Depth of Binary Tree [easy]
由于编号失误,居然漏了一个19,在这里补充上。
题目:
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.
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its depth = 3.
这个题目很简单,就是求二叉树的深度。深度是二叉树的一个属性,一个递归就可以解决,不再赘述。
# 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
else:
return 1 + max(self.maxDepth(root.left),self.maxDepth(root.right))
53ms,62.86%
2018年2月13日23:07:17
one hour before Valentine‘s Day ^_^