From 842de9d7021d00f1bce148399d65f17e03ac1c46 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Thu, 9 Jul 2020 10:35:33 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9B=B8?= =?UTF-8?q?=E5=85=B3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Algorithm/剑指 Offer/二叉树相关.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) 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; + } +} +``` +