diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index e62a3e66..a57a92aa 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1528,6 +1528,29 @@ public: ``` Java: +```Java +class Solution { + public int maxDepth(TreeNode root) { + if (root == null) return 0; + Queue que = new LinkedList<>(); + que.offer(root); + int depth = 0; + while (!que.isEmpty()) + { + int len = que.size(); + while (len > 0) + { + TreeNode node = que.poll(); + if (node.left != null) que.offer(node.left); + if (node.right != null) que.offer(node.right); + len--; + } + depth++; + } + return depth; + } +} +``` Python: