173.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict'
  2. /**
  3. * Definition for binary tree
  4. * function TreeNode(val) {
  5. * this.val = val;
  6. * this.left = this.right = null;
  7. * }
  8. */
  9. function TreeNode(val) {
  10. this.val = val;
  11. this.left = this.right = null;
  12. }
  13. /**
  14. * @constructor
  15. * @param {TreeNode} root - root of the binary search tree
  16. */
  17. var BSTIterator = function (root) {
  18. }
  19. /**
  20. * @this BSTIterator
  21. * @returns {boolean} - whether we have a next smallest number
  22. */
  23. BSTIterator.prototype.hasNext = function () {
  24. }
  25. /**
  26. * @this BSTIterator
  27. * @returns {number} - the next smallest number
  28. */
  29. BSTIterator.prototype.next = function () {
  30. }
  31. /**
  32. * Your BSTIterator will be called like this:
  33. * var i = new BSTIterator(root), a = [];
  34. * while (i.hasNext()) a.push(i.next());
  35. */
  36. function __main__() {
  37. /* eslit no-console: ["error", {"allow": ["log"]}] */
  38. const logger = console.log.bind(console)
  39. // 1
  40. // / \
  41. // 2 3
  42. // / /\
  43. // 4 6 7
  44. const n7 = new TreeLinkNode(7)
  45. const n6 = new TreeLinkNode(6)
  46. const n4 = new TreeLinkNode(4)
  47. const n3 = new TreeLinkNode(3)
  48. n3.left = n6
  49. n3.right = n7
  50. const n2 = new TreeLinkNode(2)
  51. n2.left = n4
  52. const n1 = new TreeLinkNode(1)
  53. n1.left = n2
  54. n1.right = n3
  55. const i = new BSTIterator(n1)
  56. const a = []
  57. while (i.hasNext()) {
  58. a.push(i.next())
  59. }
  60. logger(a)
  61. }
  62. __main__()