Advertisement
ogv

Untitled

ogv
Sep 10th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class BSTIterator {
  11. private Stack<TreeNode> path;
  12. private Stack<Character> operations;
  13.  
  14. public BSTIterator(TreeNode root) {
  15. path = new Stack<TreeNode>();
  16. operations = new Stack<Character>();
  17.  
  18. path.push(root);
  19. operations.push('L');
  20. }
  21.  
  22. /** @return the next smallest number */
  23. public int next() {
  24. char operation = operations.peek();
  25. switch (operation) {
  26. case 'L':
  27. TreeNode left = path.peek().left;
  28. if (left != null) {
  29. path.push(left);
  30. operations.push('L');
  31. }
  32. break;
  33. case 'M':
  34. break;
  35. case 'R':
  36. break;
  37. }
  38. return 0;
  39. }
  40.  
  41. /** @return whether we have a next smallest number */
  42. public boolean hasNext() {
  43. return false;
  44. }
  45. }
  46.  
  47. /**
  48. * Your BSTIterator object will be instantiated and called as such:
  49. * BSTIterator obj = new BSTIterator(root);
  50. * int param_1 = obj.next();
  51. * boolean param_2 = obj.hasNext();
  52. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement