qianlei90 / Blog

那些该死的文字呦
https://qianlei.notion.site
103 stars 20 forks source link

104. Maximum Depth of Binary Tree #17

Open qianlei90 opened 7 years ago

qianlei90 commented 7 years ago

104. Maximum Depth of Binary Tree

Tags: 印象笔记

[toc]


Question

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.

Solution

因为给的是二叉树,所以直接利用递归依次遍历两个子节点来计算深度就好。第一次做这样的题,如果不看讨论区里的答案,还真不知道怎么去写。

Show me the code

# 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
        """
        return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) if root else 0

- 完 -