diff --git a/blogs/Algorithm/剑指 Offer/二叉树相关.md b/blogs/Algorithm/剑指 Offer/二叉树相关.md index c76b9a6..fb81591 100644 --- a/blogs/Algorithm/剑指 Offer/二叉树相关.md +++ b/blogs/Algorithm/剑指 Offer/二叉树相关.md @@ -194,3 +194,45 @@ class Solution { } ``` +#### [55 - I. 二叉树的深度](https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/) + +```java +class Solution { + + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } +} +``` + +```java +class Solution { + + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + int result = 0; + Deque queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + result++; + int n = queue.size(); + for (int i = 0; i < n; i++) { + TreeNode node = queue.poll(); + if (node.left != null) { + queue.add(node.left); + } + if (node.right != null) { + queue.add(node.right); + } + } + } + return result; + } +} +``` +