From bf45a6f17d241a45870148af2bd5207064d25178 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Tue, 5 Mar 2024 22:08:25 +0800 Subject: [PATCH] update 104 java, progress --- data/progress.txt | 2 +- data/to_review.txt | 18 +++---- .../Recursion/MaximumDepthOfBinaryTree.java | 50 +++++++------------ 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index 90f11c6f..ea741a09 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240305: 100 +20240305: 100,102,104 20240304: 73,76,79(again),91,25 20240303: 55(again),56,62,70 20240229: 39,48(again),49,53,54 diff --git a/data/to_review.txt b/data/to_review.txt index 1d91fb18..0ddfde45 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,32 +1,32 @@ -2024-04-29 -> ['100'] +2024-04-29 -> ['100,102,104'] 2024-04-28 -> ['73,76,79(again),91,25'] 2024-04-27 -> ['55(again),56,62,70'] 2024-04-24 -> ['39,48(again),49,53,54'] 2024-04-23 -> ['20,21,23,33(again)'] 2024-04-22 -> ['1,3,5,4,19'] -2024-04-08 -> ['100'] +2024-04-08 -> ['100,102,104'] 2024-04-07 -> ['73,76,79(again),91,25'] 2024-04-06 -> ['55(again),56,62,70'] 2024-04-03 -> ['39,48(again),49,53,54'] 2024-04-02 -> ['20,21,23,33(again)'] 2024-04-01 -> ['1,3,5,4,19'] -2024-03-26 -> ['100'] +2024-03-26 -> ['100,102,104'] 2024-03-25 -> ['73,76,79(again),91,25'] 2024-03-24 -> ['55(again),56,62,70'] 2024-03-21 -> ['39,48(again),49,53,54'] 2024-03-20 -> ['20,21,23,33(again)'] 2024-03-19 -> ['1,3,5,4,19'] -2024-03-18 -> ['100'] +2024-03-18 -> ['100,102,104'] 2024-03-17 -> ['73,76,79(again),91,25'] 2024-03-16 -> ['55(again),56,62,70'] -2024-03-13 -> ['100', '39,48(again),49,53,54'] +2024-03-13 -> ['100,102,104', '39,48(again),49,53,54'] 2024-03-12 -> ['73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-11 -> ['55(again),56,62,70', '1,3,5,4,19'] -2024-03-10 -> ['100'] +2024-03-10 -> ['100,102,104'] 2024-03-09 -> ['73,76,79(again),91,25'] -2024-03-08 -> ['100', '55(again),56,62,70', '39,48(again),49,53,54'] -2024-03-07 -> ['100', '73,76,79(again),91,25', '20,21,23,33(again)'] -2024-03-06 -> ['100', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19'] +2024-03-08 -> ['100,102,104', '55(again),56,62,70', '39,48(again),49,53,54'] +2024-03-07 -> ['100,102,104', '73,76,79(again),91,25', '20,21,23,33(again)'] +2024-03-06 -> ['100,102,104', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-05 -> ['73,76,79(again),91,25', '55(again),56,62,70', '39,48(again),49,53,54'] 2024-03-04 -> ['55(again),56,62,70', '20,21,23,33(again)'] 2024-03-03 -> ['39,48(again),49,53,54', '1,3,5,4,19'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Recursion/MaximumDepthOfBinaryTree.java b/leetcode_java/src/main/java/LeetCodeJava/Recursion/MaximumDepthOfBinaryTree.java index 523d0633..74e538b6 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Recursion/MaximumDepthOfBinaryTree.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Recursion/MaximumDepthOfBinaryTree.java @@ -38,37 +38,25 @@ public int maxDepth_1(TreeNode root) { } - // V0 -// public int maxDepth(TreeNode root) { -// -// if (root == null){ -// return 0; -// } -// -// int resultL = findDepth(root.left, 0); -// int resultR = findDepth(root.right, 0); -// -// return Math.max(resultL, resultR); -// } -// -// private int findDepth(TreeNode root, int layer) { -// -// if (root == null){ -// return layer; -// } -// -// if (root.left != null){ -// layer += 1; -// findDepth(root.left, layer); -// } -// -// if (root.right != null){ -// layer += 1; -// findDepth(root.right, layer); -// } -// -// return layer; -// } + // V0'' + // IDEA : DFS (RECURSION) + public int maxDepth__(TreeNode root) { + + if (root == null){ + return 0; + } + int cur = 0; + int depth = 0; + return dfs(root, cur, depth); + } + private int dfs(TreeNode root, int cur, int depth){ + if (root == null){ + depth = Math.max(cur, depth); + return depth; + } + return Math.max(dfs(root.left, cur+1, depth), dfs(root.right, cur+1, depth)); + } + // V1 // IDEA : RECURSION