LeetCode - Medium - 173. Binary Search Tree Iterator

超、凢脫俗 2022-10-16 15:20 306阅读 0赞

Topic

  • Stack
  • Tree
  • Design

Description

https://leetcode.com/problems/binary-search-tree-iterator/

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:

743f55b78d358ff4ba5fbb7d8088a0d9.png

  1. Input
  2. ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
  3. [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
  4. Output
  5. [null, 3, 7, true, 9, true, 15, true, 20, false]
  6. Explanation
  7. BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
  8. bSTIterator.next(); // return 3
  9. bSTIterator.next(); // return 7
  10. bSTIterator.hasNext(); // return True
  11. bSTIterator.next(); // return 9
  12. bSTIterator.hasNext(); // return True
  13. bSTIterator.next(); // return 15
  14. bSTIterator.hasNext(); // return True
  15. bSTIterator.next(); // return 20
  16. bSTIterator.hasNext(); // return False

Constraints:

  • The number of nodes in the tree is in the range [ 1 , 1 0 5 ] [1, 10^5] [1,105].
  • 0 < = N o d e . v a l < = 1 0 6 0 <= Node.val <= 10^6 0<=Node.val<=106
  • At most 1 0 5 10^5 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?

Analysis

主要考察BST的中序遍历的非递归法。

方法一:我写的

方法二:别人写的

方法三:别人写的2

Submission

  1. import java.util.LinkedList;
  2. import com.lun.util.BinaryTree.TreeNode;
  3. public class BinarySearchTreeIterator {
  4. //方法一:我写的
  5. public static class BSTIterator {
  6. private LinkedList<Object[]> stack;
  7. public BSTIterator(TreeNode root) {
  8. stack = new LinkedList<>();
  9. stack.push(new Object[] { root, 0});
  10. }
  11. public int next() {
  12. while(!stack.isEmpty()) {
  13. Object[] arr = stack.peek();
  14. TreeNode node = (TreeNode)arr[0];
  15. int state = (int)arr[1];
  16. if(state == 0) {
  17. if(node.left != null)
  18. stack.push(new Object[] { node.left, 0});
  19. arr[1] = 1;
  20. }else{
  21. stack.pop();
  22. if(node.right != null)
  23. stack.push(new Object[] { node.right, 0});
  24. return node.val;
  25. }
  26. }
  27. return -1;
  28. }
  29. public boolean hasNext() {
  30. return !stack.isEmpty();
  31. }
  32. }
  33. //方法二:别人写的
  34. public static class BSTIterator2 {
  35. private LinkedList<TreeNode> stack;
  36. private TreeNode pointer;
  37. public BSTIterator2(TreeNode root) {
  38. stack = new LinkedList<>();
  39. pointer = root;
  40. }
  41. public int next() {
  42. while(hasNext()) {
  43. if(pointer != null) {
  44. stack.push(pointer);
  45. pointer = pointer.left;
  46. }else{
  47. TreeNode node = stack.pop();
  48. pointer = node.right;
  49. return node.val;
  50. }
  51. }
  52. return -1;//或抛异常
  53. }
  54. public boolean hasNext() {
  55. return !stack.isEmpty() || pointer != null;
  56. }
  57. }
  58. //方法三:别人写的2
  59. public static class BSTIterator3 {
  60. private LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
  61. public BSTIterator3(TreeNode root) {
  62. pushAll(root);
  63. }
  64. /** @return whether we have a next smallest number */
  65. public boolean hasNext() {
  66. return !stack.isEmpty();
  67. }
  68. /** @return the next smallest number */
  69. public int next() {
  70. TreeNode tmpNode = stack.pop();
  71. pushAll(tmpNode.right);
  72. return tmpNode.val;
  73. }
  74. private void pushAll(TreeNode node) {
  75. for (; node != null; stack.push(node), node = node.left);
  76. }
  77. }
  78. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.medium.BinarySearchTreeIterator.BSTIterator;
  4. import com.lun.medium.BinarySearchTreeIterator.BSTIterator2;
  5. import com.lun.medium.BinarySearchTreeIterator.BSTIterator3;
  6. import com.lun.util.BinaryTree;
  7. import com.lun.util.BinaryTree.TreeNode;
  8. public class BinarySearchTreeIteratorTest {
  9. @Test
  10. public void test() {
  11. TreeNode root = BinaryTree.integers2BinaryTree(7, 3, 15, null, null, 9, 20);
  12. BSTIterator bSTIterator = new BSTIterator(root);
  13. assertEquals(3, bSTIterator.next());
  14. assertEquals(7, bSTIterator.next());
  15. assertTrue(bSTIterator.hasNext());
  16. assertEquals(9, bSTIterator.next());
  17. assertTrue(bSTIterator.hasNext());
  18. assertEquals(15, bSTIterator.next());
  19. assertTrue(bSTIterator.hasNext());
  20. assertEquals(20, bSTIterator.next());
  21. assertFalse(bSTIterator.hasNext());
  22. }
  23. @Test
  24. public void test2() {
  25. TreeNode root = BinaryTree.integers2BinaryTree(7, 3, 15, null, null, 9, 20);
  26. BSTIterator2 bSTIterator = new BSTIterator2(root);
  27. assertEquals(3, bSTIterator.next());
  28. assertEquals(7, bSTIterator.next());
  29. assertTrue(bSTIterator.hasNext());
  30. assertEquals(9, bSTIterator.next());
  31. assertTrue(bSTIterator.hasNext());
  32. assertEquals(15, bSTIterator.next());
  33. assertTrue(bSTIterator.hasNext());
  34. assertEquals(20, bSTIterator.next());
  35. assertFalse(bSTIterator.hasNext());
  36. }
  37. @Test
  38. public void test3() {
  39. TreeNode root = BinaryTree.integers2BinaryTree(7, 3, 15, null, null, 9, 20);
  40. BSTIterator3 bSTIterator = new BSTIterator3(root);
  41. assertEquals(3, bSTIterator.next());
  42. assertEquals(7, bSTIterator.next());
  43. assertTrue(bSTIterator.hasNext());
  44. assertEquals(9, bSTIterator.next());
  45. assertTrue(bSTIterator.hasNext());
  46. assertEquals(15, bSTIterator.next());
  47. assertTrue(bSTIterator.hasNext());
  48. assertEquals(20, bSTIterator.next());
  49. assertFalse(bSTIterator.hasNext());
  50. }
  51. }

发表评论

表情:
评论列表 (有 0 条评论,306人围观)

还没有评论,来说两句吧...

相关阅读