题解 | #二叉树的最大深度#

二叉树的最大深度

http://www.nowcoder.com/practice/8a2b2bf6c19b4f23a9bdb9b233eefa73

bfs, from collections import deque

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

#
# 
# @param root TreeNode类 
# @return int整型
#
from collections import deque
class Solution:
    def maxDepth(self , root ):
        # write code here
        if not root:
            return 0
        q = deque([root])
        depth = 0
        while q:
            length = len(q)
            for i in range(length):
                curr = q.popleft()
                if curr.left:
                    q.append(curr.left)
                if curr.right:
                    q.append(curr.right)
            depth += 1
        return depth
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务