|
1 |
| -# Maximum Difference Between Node and Ancestor |
2 |
| -Given the root of a binary tree, find the maximum value V for which there exist different nodes A and B where V = |A.val - B.val| and A is an ancestor of B. |
| 1 | +# Binary Search Tree Iterator |
| 2 | +Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST): |
3 | 3 |
|
4 |
| -A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an ancestor of B. |
| 4 | +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. |
| 5 | +boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false. |
| 6 | +int next() Moves the pointer to the right, then returns the number at the pointer. |
| 7 | +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. |
| 8 | + |
| 9 | +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. |
5 | 10 |
|
6 | 11 |
|
7 | 12 |
|
8 | 13 | Example 1:
|
9 | 14 |
|
10 | 15 |
|
11 |
| -Input: root = [8,3,10,1,6,null,14,null,null,4,7,13] |
12 |
| -Output: 7 |
13 |
| -Explanation: We have various ancestor-node differences, some of which are given below : |
14 |
| -|8 - 3| = 5 |
15 |
| -|3 - 7| = 4 |
16 |
| -|8 - 1| = 7 |
17 |
| -|10 - 13| = 3 |
18 |
| -Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7. |
19 |
| -Example 2: |
20 |
| - |
| 16 | +Input |
| 17 | +["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] |
| 18 | +[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []] |
| 19 | +Output |
| 20 | +[null, 3, 7, true, 9, true, 15, true, 20, false] |
21 | 21 |
|
22 |
| -Input: root = [1,null,2,null,0,3] |
23 |
| -Output: 3 |
| 22 | +Explanation |
| 23 | +BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]); |
| 24 | +bSTIterator.next(); // return 3 |
| 25 | +bSTIterator.next(); // return 7 |
| 26 | +bSTIterator.hasNext(); // return True |
| 27 | +bSTIterator.next(); // return 9 |
| 28 | +bSTIterator.hasNext(); // return True |
| 29 | +bSTIterator.next(); // return 15 |
| 30 | +bSTIterator.hasNext(); // return True |
| 31 | +bSTIterator.next(); // return 20 |
| 32 | +bSTIterator.hasNext(); // return False |
24 | 33 |
|
25 | 34 |
|
26 | 35 | Constraints:
|
27 | 36 |
|
28 |
| -The number of nodes in the tree is in the range [2, 5000]. |
29 |
| -0 <= Node.val <= 105<br> |
| 37 | +The number of nodes in the tree is in the range [1, 105]. |
| 38 | +0 <= Node.val <= 106 |
| 39 | +At most 105 calls will be made to hasNext, and next. |
| 40 | + |
| 41 | + |
| 42 | +Follow up: |
| 43 | + |
| 44 | +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?<br> |
30 | 45 |
|
31 | 46 | ## Idea
|
32 | 47 |
|
|
0 commit comments