From d551a9429250a6299e3d52b00f6d04e8f4ba560e Mon Sep 17 00:00:00 2001 From: laozhang <1044985343@qq.com> Date: Fri, 15 May 2020 16:50:25 +0800 Subject: [PATCH] python leetcode 55 --- python-leetcode/README.md | 1 + python-leetcode/solution/tree/leetcode_55_.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 python-leetcode/solution/tree/leetcode_55_.py diff --git a/python-leetcode/README.md b/python-leetcode/README.md index d1480ac0..180b7d6b 100644 --- a/python-leetcode/README.md +++ b/python-leetcode/README.md @@ -8,3 +8,4 @@ ## 二叉树专题 1. [二叉树的镜像](./solution/tree/leetcode_27_.py) +1. [二叉树的深度](./solution/tree/leetcode_55_.py) diff --git a/python-leetcode/solution/tree/leetcode_55_.py b/python-leetcode/solution/tree/leetcode_55_.py new file mode 100644 index 00000000..1e5385ec --- /dev/null +++ b/python-leetcode/solution/tree/leetcode_55_.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# coding=utf-8 +""" +27. 二叉树的镜像 +""" +from solution import TreeNode + + +class Solution: + def maxDepth(self, root: TreeNode) -> int: + if root is None: + return 0 + leftDepth = self.maxDepth(root.left) + 1 + rightDepth = self.maxDepth(root.right) + 1 + return leftDepth if leftDepth > rightDepth else rightDepth -- GitLab