From a8ef4fe6eeea4869dac2361566e3ed11e3d8d6a6 Mon Sep 17 00:00:00 2001 From: hailincai Date: Fri, 29 Oct 2021 08:16:18 -0400 Subject: [PATCH] =?UTF-8?q?Update=200104.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加559N Java 递归方法 --- problems/0104.二叉树的最大深度.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index e20f147f..ff7cbfd1 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -312,6 +312,24 @@ class solution { ``` ### 559.n叉树的最大深度 +```java +class Solution { + /*递归法,后序遍历求root节点的高度*/ + public int maxDepth(Node root) { + if (root == null) return 0; + + int depth = 0; + if (root.children != null){ + for (Node child : root.children){ + depth = Math.max(depth, maxDepth(child)); + } + } + + return depth + 1; //中节点 + } +} +``` + ```java class solution { /**