@@ -419,86 +419,107 @@ class solution:
|
||||
return 1 + max(self.maxdepth(root.left), self.maxdepth(root.right))
|
||||
```
|
||||
|
||||
迭代法:
|
||||
层序遍历迭代法:
|
||||
```python
|
||||
import collections
|
||||
class solution:
|
||||
def maxdepth(self, root: treenode) -> int:
|
||||
# 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 maxDepth(self, root: TreeNode) -> int:
|
||||
if not root:
|
||||
return 0
|
||||
depth = 0 #记录深度
|
||||
queue = collections.deque()
|
||||
queue.append(root)
|
||||
|
||||
depth = 0
|
||||
queue = collections.deque([root])
|
||||
|
||||
while queue:
|
||||
size = len(queue)
|
||||
depth += 1
|
||||
for i in range(size):
|
||||
for _ in range(len(queue)):
|
||||
node = queue.popleft()
|
||||
if node.left:
|
||||
queue.append(node.left)
|
||||
if node.right:
|
||||
queue.append(node.right)
|
||||
|
||||
return depth
|
||||
|
||||
```
|
||||
|
||||
### 559.n叉树的最大深度
|
||||
|
||||
递归法:
|
||||
```python
|
||||
class solution:
|
||||
def maxdepth(self, root: 'node') -> int:
|
||||
class Solution:
|
||||
def maxDepth(self, root: 'Node') -> int:
|
||||
if not root:
|
||||
return 0
|
||||
depth = 0
|
||||
for i in range(len(root.children)):
|
||||
depth = max(depth, self.maxdepth(root.children[i]))
|
||||
return depth + 1
|
||||
|
||||
max_depth = 1
|
||||
|
||||
for child in root.children:
|
||||
max_depth = max(max_depth, self.maxDepth(child) + 1)
|
||||
|
||||
return max_depth
|
||||
```
|
||||
|
||||
迭代法:
|
||||
```python
|
||||
import collections
|
||||
class solution:
|
||||
def maxdepth(self, root: 'node') -> int:
|
||||
queue = collections.deque()
|
||||
if root:
|
||||
queue.append(root)
|
||||
depth = 0 #记录深度
|
||||
"""
|
||||
# Definition for a Node.
|
||||
class Node:
|
||||
def __init__(self, val=None, children=None):
|
||||
self.val = val
|
||||
self.children = children
|
||||
"""
|
||||
|
||||
class Solution:
|
||||
def maxDepth(self, root: TreeNode) -> int:
|
||||
if not root:
|
||||
return 0
|
||||
|
||||
depth = 0
|
||||
queue = collections.deque([root])
|
||||
|
||||
while queue:
|
||||
size = len(queue)
|
||||
depth += 1
|
||||
for i in range(size):
|
||||
for _ in range(len(queue)):
|
||||
node = queue.popleft()
|
||||
for j in range(len(node.children)):
|
||||
if node.children[j]:
|
||||
queue.append(node.children[j])
|
||||
for child in node.children:
|
||||
queue.append(child)
|
||||
|
||||
return depth
|
||||
|
||||
```
|
||||
|
||||
使用栈来模拟后序遍历依然可以
|
||||
使用栈
|
||||
```python
|
||||
class solution:
|
||||
def maxdepth(self, root: 'node') -> int:
|
||||
st = []
|
||||
if root:
|
||||
st.append(root)
|
||||
depth = 0
|
||||
result = 0
|
||||
while st:
|
||||
node = st.pop()
|
||||
if node != none:
|
||||
st.append(node) #中
|
||||
st.append(none)
|
||||
depth += 1
|
||||
for i in range(len(node.children)): #处理孩子
|
||||
if node.children[i]:
|
||||
st.append(node.children[i])
|
||||
|
||||
else:
|
||||
node = st.pop()
|
||||
depth -= 1
|
||||
result = max(result, depth)
|
||||
return result
|
||||
"""
|
||||
# Definition for a Node.
|
||||
class Node:
|
||||
def __init__(self, val=None, children=None):
|
||||
self.val = val
|
||||
self.children = children
|
||||
"""
|
||||
|
||||
class Solution:
|
||||
def maxDepth(self, root: 'Node') -> int:
|
||||
if not root:
|
||||
return 0
|
||||
|
||||
max_depth = 0
|
||||
|
||||
stack = [(root, 1)]
|
||||
|
||||
while stack:
|
||||
node, depth = stack.pop()
|
||||
max_depth = max(max_depth, depth)
|
||||
for child in node.children:
|
||||
stack.append((child, depth + 1))
|
||||
|
||||
return max_depth
|
||||
```
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user