diff --git a/README.md b/README.md index 36b613c0..9b82e4ca 100644 --- a/README.md +++ b/README.md @@ -466,7 +466,7 @@ | # | Title | Solution | Time | Space | Difficulty | Note | Status| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 094 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Python](./leetcode_python/Tree/binary-tree-inorder-traversal.py) | _O(n)_| _O(1)_| Medium |tree, iteration, recursion, `good basic`,`Morris Traversal`, `M$`, `fb` | AGAIN********* (4) (MUST) -124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [Python](./leetcode_python/Tree/binary-tree-maximum-path-sum.py) | _O(n)_| _O(1)_| Hard |Curated Top 75, google, amazon, fb, m$, twitter | AGAIN******* (2) +124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [Python](./leetcode_python/Tree/binary-tree-maximum-path-sum.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Tree/BinaryTreeMaximumPathSum.java) | _O(n)_| _O(1)_| Hard |Curated Top 75, google, amazon, fb, m$, twitter | AGAIN******* (2) 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Python](./leetcode_python/Tree/binary-tree-preorder-traversal.py) | _O(n)_| _O(1)_| Medium |`Morris Traversal`| AGAIN 208 | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) | [Python](./leetcode_python/Tree/implement-trie-prefix-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Trie/ImplementTrie.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, dict tree, LC 211, trie, `amazon`,`fb`| AGAIN************** (7) (MUST) 211 | [Design Add and Search Words Data Structure](https://leetcode.com/problems/add-and-search-word-data-structure-design/) | [Python](./leetcode_python/Tree/add-and-search-word-data-structure-design.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Trie/DesignAddAndSearchWordsDataStructure.java) | _O(min(n, h))_ | _O(min(n, h))_ | Medium |Curated Top 75, `Trie`, recursive, node, hashMap, do `# 208 first`,`amazon` ,`fb`| AGAIN********* (7) (MUST) diff --git a/data/progress.txt b/data/progress.txt index e65ce54e..39784f57 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240306: 121 +20240306: 121,124(again) 20240305: 100,102,104,105(again) 20240304: 73,76,79(again),91,25 20240303: 55(again),56,62,70 diff --git a/data/to_review.txt b/data/to_review.txt index ea03bd23..d7b35834 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,31 +1,35 @@ +2024-04-30 -> ['121,124(again)'] 2024-04-29 -> ['100,102,104,105(again)'] 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-09 -> ['121,124(again)'] 2024-04-08 -> ['100,102,104,105(again)'] 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-27 -> ['121,124(again)'] 2024-03-26 -> ['100,102,104,105(again)'] 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-19 -> ['121,124(again)', '1,3,5,4,19'] 2024-03-18 -> ['100,102,104,105(again)'] 2024-03-17 -> ['73,76,79(again),91,25'] 2024-03-16 -> ['55(again),56,62,70'] +2024-03-14 -> ['121,124(again)'] 2024-03-13 -> ['100,102,104,105(again)', '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-11 -> ['121,124(again)', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-10 -> ['100,102,104,105(again)'] -2024-03-09 -> ['73,76,79(again),91,25'] -2024-03-08 -> ['100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] -2024-03-07 -> ['100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] +2024-03-09 -> ['121,124(again)', '73,76,79(again),91,25'] +2024-03-08 -> ['121,124(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] +2024-03-07 -> ['121,124(again)', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-06 -> ['100,102,104,105(again)', '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)'] @@ -198,7 +202,7 @@ 2023-06-30 -> ['637,653', '1047', '228,243'] 2023-06-29 -> ['104,110', '162'] 2023-06-28 -> ['104,110'] -2023-06-27 -> ['121', '104,110', '162', '160,203'] +2023-06-27 -> ['104,110', '162', '160,203', '121'] 2023-06-26 -> ['162', '687', '617', '1353,21,24', '66'] 2023-06-25 -> ['162', '637,653', '325,26'] 2023-06-24 -> ['543,572', '49,246'] @@ -219,7 +223,7 @@ 2023-06-09 -> ['160,203', '1047', '228,243'] 2023-06-08 -> ['160,203', '1353,21,24'] 2023-06-07 -> ['160,203', '1353,21,24'] -2023-06-06 -> ['121', '1353,21,24'] +2023-06-06 -> ['1353,21,24', '121'] 2023-06-05 -> ['66'] 2023-06-04 -> ['1047', '101', '325,26'] 2023-06-03 -> ['067', '49,246'] @@ -239,15 +243,15 @@ 2023-05-19 -> ['101', '228,243'] 2023-05-18 -> ['067'] 2023-05-17 -> ['101'] -2023-05-16 -> ['121', '101', '067', '038'] +2023-05-16 -> ['101', '067', '038', '121'] 2023-05-15 -> ['101', '067', '674', '66'] 2023-05-14 -> ['067', '228,243', '325,26'] 2023-05-13 -> ['038', '49,246'] 2023-05-12 -> ['674'] -2023-05-11 -> ['121', '038', '228,243'] +2023-05-11 -> ['038', '228,243', '121'] 2023-05-10 -> ['038', '674', '66'] 2023-05-09 -> ['038', '674', '228,243', '325,26'] -2023-05-08 -> ['121', '674', '228,243', '49,246'] +2023-05-08 -> ['674', '228,243', '121', '49,246'] 2023-05-07 -> ['228,243', '66'] 2023-05-06 -> ['121', '325,26'] 2023-05-05 -> ['121', '66', '49,246'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Tree/BinaryTreeMaximumPathSum.java b/leetcode_java/src/main/java/LeetCodeJava/Tree/BinaryTreeMaximumPathSum.java new file mode 100644 index 00000000..26350d0b --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Tree/BinaryTreeMaximumPathSum.java @@ -0,0 +1,80 @@ +package LeetCodeJava.Tree; + +// https://leetcode.com/problems/binary-tree-maximum-path-sum/description/ + +import LeetCodeJava.DataStructure.TreeNode; + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ + +public class BinaryTreeMaximumPathSum { + + // V0 + // TODO : implement +// public int maxPathSum(TreeNode root) { +// +// return 0; +// } + + + // V1 + // IDEA : DFS + // https://leetcode.com/problems/binary-tree-maximum-path-sum/solutions/4586190/beat-100-1/ + int max = Integer.MIN_VALUE; + + public int maxPath_1(TreeNode root) { + + if(root == null) return 0; + + int value = root.val; + + int left_sum = Math.max(maxPath_1(root.left),0); + int right_sum = Math.max(maxPath_1(root.right),0); + + max = Math.max(max, left_sum + right_sum + value); + + return Math.max(left_sum, right_sum) + value; + } + + public int maxPathSum_1(TreeNode root) { + + maxPath_1(root); + return max; + } + + // V2 + // IDEA : DFS + // https://leetcode.com/problems/binary-tree-maximum-path-sum/solutions/4538150/simple-java-solution-dfs/ + class INT { + int val; + } + public int maxPathSum_2(TreeNode root) { + INT maxPathLen = new INT(); + maxPathLen.val = Integer.MIN_VALUE; + dfs_2(root, maxPathLen); + return maxPathLen.val; + } + + int dfs_2(TreeNode root, INT maxLen) { + if(root == null) + return 0; + int left = Math.max(dfs_2(root.left, maxLen), 0); + int right = Math.max(dfs_2(root.right, maxLen), 0); + maxLen.val = Math.max(maxLen.val, root.val + left + right); + return root.val + Math.max(left, right); + } + +}