From ca0c83501d5955b2e437eaed4c9e63645c765cc3 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 28 Dec 2024 17:22:07 +0800 Subject: [PATCH] add 173 java --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +- .../Stack/BinarySearchTreeIterator.java | 227 ++++++++++++++++++ .../src/main/java/dev/workspace6.java | 53 ++++ 5 files changed, 291 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/Stack/BinarySearchTreeIterator.java diff --git a/README.md b/README.md index 0589cc65..83524b81 100644 --- a/README.md +++ b/README.md @@ -437,7 +437,7 @@ 101| [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)| [Python](./leetcode_python/Stack/symmetric-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/SymmetricTree.java) | _O(n)_ | _O(h)_ | Easy |`good basic`,`bfs`, `dfs`, `linkedin`, `M$`,`amazon`, `fb`, google| AGAIN********** (5) 150| [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| [Python](./leetcode_python/Stack/evaluate-reverse-polish-notation.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/EvaluateReversePolishNotation.java)| _O(n)_| _O(n)_| Medium |stack, good trick,`amazon`| AGAIN********* (5) 155| [Min Stack](https://leetcode.com/problems/min-stack/) | [Python](./leetcode_python/Stack/min-stack.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/MinStack_.java) | _O(n)_ | _O(1)_ | Medium |basic, stack, data structure, `amazon`| OK******* (5) -173| [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)| [Python](./leetcode_python/Stack/binary-search-tree-iterator.py) | _O(1)_| _O(h)_| Medium |`good basic`,`tree, stack, M$`, `linkedin`, `google`,`amazon`,`fb`| OK***** (5) +173| [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)| [Python](./leetcode_python/Stack/binary-search-tree-iterator.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/BinarySearchTreeIterator.java)| _O(1)_| _O(h)_| Medium |`good basic`, stack,`tree, stack, M$`, `linkedin`, `google`,`amazon`,`fb`, google| OK***** (5) 224| [Basic Calculator](https://leetcode.com/problems/basic-calculator/) |[Python](./leetcode_python/Stack/basic-calculator.py)| _O(n)_| _O(n)_| Hard |`basic`,`trick`,`stack`, LC 227, amazon| AGAIN******* (3) 227| [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/) |[Python](./leetcode_python/Stack/basic-calculator-ii.py)| _O(n)_| _O(n)_| Medium |delay op, LC 224, `good trick`,`stack`,`airbnb` ,`fb`, amazon| AGAIN************** (6) (MUST) 232| [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | [Python](./leetcode_python/Stack/implement-queue-using-stacks.py) | _O(1), amortized_| _O(n)_| Easy | stack, stack-queue, EPI, LintCode, `amazon`| OK** (2) diff --git a/data/progress.txt b/data/progress.txt index fa3ea2fe..ae508908 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20241228: 379 +20241228: 379,173 20241227: 079,212(todo),362,849,855(todo) 20241222: 369,311 20241221: 370 diff --git a/data/to_review.txt b/data/to_review.txt index 51bcc538..f467781f 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,11 +1,11 @@ -2025-02-21 -> ['379'] +2025-02-21 -> ['379,173'] 2025-02-20 -> ['079,212(todo),362,849,855(todo)'] 2025-02-15 -> ['369,311'] 2025-02-14 -> ['370'] 2025-02-13 -> ['815,871,593,1109'] 2025-02-07 -> ['560,523'] 2025-02-01 -> ['304,853,325'] -2025-01-31 -> ['379'] +2025-01-31 -> ['379,173'] 2025-01-30 -> ['079,212(todo),362,849,855(todo)'] 2025-01-26 -> ['370(todo)'] 2025-01-25 -> ['369,311'] @@ -13,23 +13,23 @@ 2025-01-23 -> ['815,871,593,1109'] 2025-01-20 -> ['722,380'] 2025-01-19 -> ['33,81'] -2025-01-18 -> ['379'] +2025-01-18 -> ['379,173'] 2025-01-17 -> ['079,212(todo),362,849,855(todo)', '560,523', '253'] 2025-01-16 -> ['776,31'] 2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)'] 2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)'] 2025-01-12 -> ['369,311'] 2025-01-11 -> ['370', '304,853,325', '394'] -2025-01-10 -> ['379', '815,871,593,1109', '833,950'] +2025-01-10 -> ['379,173', '815,871,593,1109', '833,950'] 2025-01-09 -> ['079,212(todo),362,849,855(todo)'] -2025-01-05 -> ['379', '370(todo)'] +2025-01-05 -> ['379,173', '370(todo)'] 2025-01-04 -> ['079,212(todo),362,849,855(todo)', '369,311', '560,523', '53,210,207'] 2025-01-03 -> ['370', '34,767', '444'] -2025-01-02 -> ['379', '815,871,593,1109', '1188,130,855(again)'] +2025-01-02 -> ['379,173', '815,871,593,1109', '1188,130,855(again)'] 2025-01-01 -> ['079,212(todo),362,849,855(todo)'] -2024-12-31 -> ['379'] -2024-12-30 -> ['379', '079,212(todo),362,849,855(todo)', '369,311', '722,380'] -2024-12-29 -> ['379', '079,212(todo),362,849,855(todo)', '370', '304,853,325', '33,81'] +2024-12-31 -> ['379,173'] +2024-12-30 -> ['379,173', '079,212(todo),362,849,855(todo)', '369,311', '722,380'] +2024-12-29 -> ['379,173', '079,212(todo),362,849,855(todo)', '370', '304,853,325', '33,81'] 2024-12-28 -> ['079,212(todo),362,849,855(todo)', '815,871,593,1109', '900'] 2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26'] 2024-12-26 -> ['370', '776,31'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Stack/BinarySearchTreeIterator.java b/leetcode_java/src/main/java/LeetCodeJava/Stack/BinarySearchTreeIterator.java new file mode 100644 index 00000000..e20629a5 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Stack/BinarySearchTreeIterator.java @@ -0,0 +1,227 @@ +package LeetCodeJava.Stack; + +// https://leetcode.com/problems/binary-search-tree-iterator/description/ + +import LeetCodeJava.DataStructure.TreeNode; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * 173. Binary Search Tree Iterator + * Solved + * Medium + * Topics + * Companies + * Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST): + * + * BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST. + * boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false. + * int next() Moves the pointer to the right, then returns the number at the pointer. + * Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST. + * + * You may assume that next() calls will always be valid. That is, there will be at least a next number in the in-order traversal when next() is called. + * + * + * + * Example 1: + * + * + * Input + * ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] + * [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []] + * Output + * [null, 3, 7, true, 9, true, 15, true, 20, false] + * + * Explanation + * BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]); + * bSTIterator.next(); // return 3 + * bSTIterator.next(); // return 7 + * bSTIterator.hasNext(); // return True + * bSTIterator.next(); // return 9 + * bSTIterator.hasNext(); // return True + * bSTIterator.next(); // return 15 + * bSTIterator.hasNext(); // return True + * bSTIterator.next(); // return 20 + * bSTIterator.hasNext(); // return False + * + * + * Constraints: + * + * The number of nodes in the tree is in the range [1, 105]. + * 0 <= Node.val <= 106 + * At most 105 calls will be made to hasNext, and next. + * + * + * Follow up: + * + * Could you implement next() and hasNext() to run in average O(1) time and use O(h) memory, where h is the height of the tree? + * + */ +/** + * NOTE !!! + * + * -> in-order traversal retrieves the keys in ascending sorted order. + */ +public class BinarySearchTreeIterator { + /** + * 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; + * } + * } + */ + /** + * Your BSTIterator object will be instantiated and called as such: + * BSTIterator obj = new BSTIterator(root); + * int param_1 = obj.next(); + * boolean param_2 = obj.hasNext(); + */ + // V0 + // IDEA : tree traversal + list + sorting + class BSTIterator { + + // attr + TreeNode treeNode; + List cache; + + public BSTIterator(TreeNode root) { + this.treeNode = root; + this.cache = new ArrayList<>(); + this.getValues(root); + // ordering (ascending order) + this.cache.sort(Integer::compareTo); // ??? + } + + public int next() { + int tmp = this.cache.get(0); + this.cache.remove(0); + return tmp; + } + + public boolean hasNext() { + return !this.cache.isEmpty(); + } + + private void getValues(TreeNode root){ + if (root == null){ + return; // ? + } + // pre-order traversal (root -> left -> right) + this.cache.add(root.val); + + if (root.left != null){ + this.getValues(root.left); + } + if (root.right != null){ + this.getValues(root.right); + } + } + } + + // V1 + // IDEA: STACK + // https://leetcode.com/problems/binary-search-tree-iterator/solutions/52647/nice-comparison-and-short-solution-by-st-jcmg/ + public class BSTIterator_1 { + + private TreeNode visit; + private Stack stack; + + public BSTIterator_1(TreeNode root) { + visit = root; + stack = new Stack(); + } + + public boolean hasNext() { + return visit != null || !stack.empty(); + } + + public int next() { + while (visit != null) { + stack.push(visit); + visit = visit.left; + } + TreeNode next = stack.pop(); + visit = next.right; + return next.val; + } + } + + // V2 + // https://leetcode.com/problems/binary-search-tree-iterator/solutions/52525/my-solutions-in-3-languages-with-stack-b-ktax/ + // IDEA: STACK + public class BSTIterator_2 { + private Stack stack = new Stack(); + + public BSTIterator_2(TreeNode root) { + pushAll(root); + } + + /** @return whether we have a next smallest number */ + public boolean hasNext() { + return !stack.isEmpty(); + } + + /** @return the next smallest number */ + public int next() { + TreeNode tmpNode = stack.pop(); + pushAll(tmpNode.right); + return tmpNode.val; + } + + private void pushAll(TreeNode node) { + for (; node != null; stack.push(node), node = node.left); + } + } + + // V3 + // https://leetcode.com/problems/binary-search-tree-iterator/solutions/1965120/easy-iterative-using-stack-space-oh-inst-0ov7/ + // IDEA : STACK + /** + * IDEA: + * + * 1. Brute force way - You can do in-order traversal and + * put each element into an ArrayList (additional space). + * Then use that to check hasNext() or next() element. + * However, that approach will use extra space of O(n). + * + * 2. This approach: + * a) Here we will use our own Stack (basically do in-order traversal Iteratively, + * instead of recursively). Reason, being we have more control here and no need to use extra space of O(n) for ArrayList to store all elements first. + * b) We get space of O(h) only instead of O(n). [h: is height of tree] + * + */ + class BSTIterator_3 { + Stack stack; + public BSTIterator_3(TreeNode root) { + stack = new Stack<>(); + TreeNode node = root; + updateStack(node); // update stack + } + public int next() { + TreeNode toRemove = stack.pop(); + updateStack(toRemove.right); // before return node, first update stack further + return toRemove.val; + } + public boolean hasNext() { + return !stack.isEmpty(); + } + // ------------------- + public void updateStack(TreeNode node){ + while(node != null){ + stack.add(node); + node = node.left; + } + } + } + +} diff --git a/leetcode_java/src/main/java/dev/workspace6.java b/leetcode_java/src/main/java/dev/workspace6.java index 195b9156..a38f35b6 100644 --- a/leetcode_java/src/main/java/dev/workspace6.java +++ b/leetcode_java/src/main/java/dev/workspace6.java @@ -2,6 +2,8 @@ import LeetCodeJava.DataStructure.ListNode; +import LeetCodeJava.DataStructure.TreeNode; + import java.util.*; public class workspace6 { @@ -854,4 +856,55 @@ public void release(int number) { } } + // LC 173 + // https://leetcode.com/problems/binary-search-tree-iterator/ + // 4.58 - 5.20 pm + /** + * In a binary search tree ordered such that in each node the + * key is greater than all keys in its left subtree and + * less than all keys in its right subtree, + * + * -> in-order traversal retrieves the keys in ascending sorted order. + * + */ + class BSTIterator { + + // attr + TreeNode treeNode; + List cache; + + public BSTIterator(TreeNode root) { + this.treeNode = root; + this.cache = new ArrayList<>(); + this.getValues(root); + // ordering (ascending order) + this.cache.sort(Integer::compareTo); // ??? + } + + public int next() { + int tmp = this.cache.get(0); + this.cache.remove(0); + return tmp; + } + + public boolean hasNext() { + return !this.cache.isEmpty(); + } + + private void getValues(TreeNode root){ + if (root == null){ + return; // ? + } + // pre-order traversal (root -> left -> right) + this.cache.add(root.val); + + if (root.left != null){ + this.getValues(root.left); + } + if (root.right != null){ + this.getValues(root.right); + } + } + } + }