1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: def helper(root, depth): if not root: return depth else: return max(helper(root.left, depth+1), helper(root.right, depth+1)) return helper(root, 0)
class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if not root: return 0 queue, res = [root], 0 while queue: tmp = [] for node in queue: if node.left: tmp.append(node.left) if node.right: tmp.append(node.right) queue = tmp res += 1 return res
|