From ba081f84a85ff75134c4db551e0e631b86693081 Mon Sep 17 00:00:00 2001 From: AronJudge <2286381138@qq.com> Date: Tue, 12 Jul 2022 11:36:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200104.=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 代码错误,更正代码,通过LeetCode代码检验。 --- problems/0104.二叉树的最大深度.md | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index ed27f95d..55980189 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -294,14 +294,13 @@ class solution { /** * 递归法 */ - public int maxdepth(treenode root) { + public int maxDepth(TreeNode root) { if (root == null) { return 0; } - int leftdepth = maxdepth(root.left); - int rightdepth = maxdepth(root.right); - return math.max(leftdepth, rightdepth) + 1; - + int leftDepth = maxDepth(root.left); + int rightDepth = maxDepth(root.right); + return Math.max(leftDepth, rightDepth) + 1; } } ``` @@ -311,23 +310,23 @@ class solution { /** * 迭代法,使用层序遍历 */ - public int maxdepth(treenode root) { + public int maxDepth(TreeNode root) { if(root == null) { return 0; } - deque deque = new linkedlist<>(); + Deque deque = new LinkedList<>(); deque.offer(root); int depth = 0; - while (!deque.isempty()) { + while (!deque.isEmpty()) { int size = deque.size(); depth++; for (int i = 0; i < size; i++) { - treenode poll = deque.poll(); - if (poll.left != null) { - deque.offer(poll.left); + TreeNode node = deque.poll(); + if (node.left != null) { + deque.offer(node.left); } - if (poll.right != null) { - deque.offer(poll.right); + if (node.right != null) { + deque.offer(node.right); } } }