leecode104. Maximum Depth of Binary Tree树高
Example:
Given binary tree [3,9,20,null,null,15,7],
*** 3
*** / \
** 9 20
**** / \
** 15 7
return its depth = 3.
#迭代
class Solution:
def maxDepth(self, root: TreeNode) -> int:
"""
:type root: TreeNode
:rtype: int
"""
depth = 0
level = [root] if root else []
while level:
depth += 1
queue = []
for el in level:
if el.left:
queue.append(el.left)
if el.right:
queue.append(el.right)
level = queue
return depth
#递归
class Solution:
def maxDepth(self, root: TreeNode) -> int:
return max(map(self.maxDepth,(root.left,root,right)))+1 if root else 0