-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111.二叉树的最小深度.py
More file actions
57 lines (52 loc) · 1.23 KB
/
111.二叉树的最小深度.py
File metadata and controls
57 lines (52 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#
# @lc app=leetcode.cn id=111 lang=python
#
# [111] 二叉树的最小深度
#
# https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
#
# algorithms
# Easy (40.38%)
# Likes: 165
# Dislikes: 0
# Total Accepted: 34.2K
# Total Submissions: 84.8K
# Testcase Example: '[3,9,20,null,null,15,7]'
#
# 给定一个二叉树,找出其最小深度。
#
# 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
#
# 说明: 叶子节点是指没有子节点的节点。
#
# 示例:
#
# 给定二叉树 [3,9,20,null,null,15,7],
#
# 3
# / \
# 9 20
# / \
# 15 7
#
# 返回它的最小深度 2.
#
#
# @lc code=start
# 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 minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:return 0
if not root.left:return self.minDepth(root.right)+1
if not root.right:return self.minDepth(root.left)+1
return min(self.minDepth(root.left),self.minDepth(root.right))+1
# @lc code=end