DFS
二叉树的遍历
class Solution(object):
def maxDepth_dfs(self, root):
if not root:
return 0
queue = [root]
height = 0
while queue:
currentSize = len(queue)
node = queue.pop(0)
print(node)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return height
作者:zhi-xiao-3
链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/solution/by-zhi-xiao-3-lk8r/
BFS
二叉树的遍历
class Solution(object):
def maxDepth_bfs(self, root):
if not root:
return 0
queue = [root]
height = 0
while queue:
currentSize = len(queue)
node = queue.pop()
print(node)
if node.right:
queue.append(node.right)
if node.left:
queue.append(node.left)
return height
作者:zhi-xiao-3
链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/solution/by-zhi-xiao-3-lk8r/