0
点赞
收藏
分享

微信扫一扫

104.二叉树的最大深度


104.二叉树的最大深度_子树

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
stack = []
if root is not None:
stack.append((1, root))

depth = 0
while stack != []:
current_depth, root = stack.pop()
if root is not None:
depth = max(depth, current_depth)
stack.append((current_depth + 1, root.left))
stack.append((current_depth + 1, root.right))
return depth

1.append是可以添加元组的,而且元组是可以直接拆开赋值,元组和列表的区别就是值不可改变,当你不希望别人改变你列表中的数值的时候可以使用元组

2.从递归的思想出发,每步可以简单理解为找左子树和右子树中最大的depth

3.数据结构高分笔记的图的深度优先搜索算法可以看来参考

4.rtype:是返回类型缩写



举报

相关推荐

0 条评论