mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Update 0111.二叉树的最小深度.md
This commit is contained in:
@ -297,36 +297,72 @@ class Solution {
|
||||
|
||||
## Python
|
||||
|
||||
递归法:
|
||||
递归法(版本一)
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def minDepth(self, root: TreeNode) -> int:
|
||||
if not root:
|
||||
def getDepth(self, node):
|
||||
if node is None:
|
||||
return 0
|
||||
leftDepth = self.getDepth(node.left) # 左
|
||||
rightDepth = self.getDepth(node.right) # 右
|
||||
|
||||
if not root.left and not root.right:
|
||||
return 1
|
||||
# 当一个左子树为空,右不为空,这时并不是最低点
|
||||
if node.left is None and node.right is not None:
|
||||
return 1 + rightDepth
|
||||
|
||||
left_depth = float('inf')
|
||||
right_depth = float('inf')
|
||||
# 当一个右子树为空,左不为空,这时并不是最低点
|
||||
if node.left is not None and node.right is None:
|
||||
return 1 + leftDepth
|
||||
|
||||
if root.left:
|
||||
left_depth = self.minDepth(root.left)
|
||||
if root.right:
|
||||
right_depth = self.minDepth(root.right)
|
||||
|
||||
return 1 + min(left_depth, right_depth)
|
||||
result = 1 + min(leftDepth, rightDepth)
|
||||
return result
|
||||
|
||||
def minDepth(self, root):
|
||||
return self.getDepth(root)
|
||||
|
||||
```
|
||||
递归法(版本二)
|
||||
|
||||
迭代法:
|
||||
```python
|
||||
class Solution:
|
||||
def minDepth(self, root):
|
||||
if root is None:
|
||||
return 0
|
||||
if root.left is None and root.right is not None:
|
||||
return 1 + self.minDepth(root.right)
|
||||
if root.left is not None and root.right is None:
|
||||
return 1 + self.minDepth(root.left)
|
||||
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
|
||||
|
||||
|
||||
```
|
||||
递归法(版本三)前序
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.result = float('inf')
|
||||
|
||||
def getDepth(self, node, depth):
|
||||
if node is None:
|
||||
return
|
||||
if node.left is None and node.right is None:
|
||||
self.result = min(self.result, depth)
|
||||
if node.left:
|
||||
self.getDepth(node.left, depth + 1)
|
||||
if node.right:
|
||||
self.getDepth(node.right, depth + 1)
|
||||
|
||||
def minDepth(self, root):
|
||||
if root is None:
|
||||
return 0
|
||||
self.getDepth(root, 1)
|
||||
return self.result
|
||||
|
||||
|
||||
```
|
||||
迭代法
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
@ -359,39 +395,7 @@ class Solution:
|
||||
return depth
|
||||
```
|
||||
|
||||
迭代法:
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
|
||||
class Solution:
|
||||
def minDepth(self, root: TreeNode) -> int:
|
||||
if not root:
|
||||
return 0
|
||||
|
||||
queue = collections.deque([(root, 1)])
|
||||
|
||||
while queue:
|
||||
node, depth = queue.popleft()
|
||||
|
||||
# Check if the node is a leaf node
|
||||
if not node.left and not node.right:
|
||||
return depth
|
||||
|
||||
# Add left and right child to the queue
|
||||
if node.left:
|
||||
queue.append((node.left, depth+1))
|
||||
if node.right:
|
||||
queue.append((node.right, depth+1))
|
||||
|
||||
return 0
|
||||
|
||||
```
|
||||
|
||||
## Go
|
||||
|
||||
|
Reference in New Issue
Block a user