diff --git a/problems/104.maximum-depth-of-binary-tree.md b/problems/104.maximum-depth-of-binary-tree.md index 8e59ab119..a52654195 100644 --- a/problems/104.maximum-depth-of-binary-tree.md +++ b/problems/104.maximum-depth-of-binary-tree.md @@ -49,7 +49,7 @@ var maxDepth = function(root) { - 树的基本操作- 遍历 - 层次遍历(BFS) ## 代码 -* 语言支持:JS,C++ +* 语言支持:JS,C++,Python JavaScript Code: ```js @@ -130,6 +130,23 @@ public: } }; ``` + +Python Code: +```python +class Solution: + def maxDepth(self, root: TreeNode) -> int: + if not root: return 0 + q, depth = [root, None], 1 + while q: + node = q.pop(0) + if node: + if node.left: q.append(node.left) + if node.right: q.append(node.right) + elif q: + q.append(None) + depth += 1 + return depth +``` ## 相关题目 - [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md) - [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)