|
| 1 | +/** |
| 2 | + * [173] Binary Search Tree Iterator |
| 3 | + * |
| 4 | + * Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. |
| 5 | + * |
| 6 | + * Calling next() will return the next smallest number in the BST. |
| 7 | + * |
| 8 | + * |
| 9 | + * |
| 10 | + * |
| 11 | + * |
| 12 | + * |
| 13 | + * Example: |
| 14 | + * |
| 15 | + * <img alt="" src="https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png" style="width: 189px; height: 178px;" /> |
| 16 | + * |
| 17 | + * |
| 18 | + * BSTIterator iterator = new BSTIterator(root); |
| 19 | + * iterator.next(); // return 3 |
| 20 | + * iterator.next(); // return 7 |
| 21 | + * iterator.hasNext(); // return true |
| 22 | + * iterator.next(); // return 9 |
| 23 | + * iterator.hasNext(); // return true |
| 24 | + * iterator.next(); // return 15 |
| 25 | + * iterator.hasNext(); // return true |
| 26 | + * iterator.next(); // return 20 |
| 27 | + * iterator.hasNext(); // return false |
| 28 | + * |
| 29 | + * |
| 30 | + * |
| 31 | + * |
| 32 | + * Note: |
| 33 | + * |
| 34 | + * |
| 35 | + * next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. |
| 36 | + * You may assume that next() call will always be valid, that is, there will be at least a next smallest number in the BST when next() is called. |
| 37 | + * |
| 38 | + * |
| 39 | + */ |
| 40 | +pub struct Solution {} |
| 41 | +use super::util::tree::{TreeNode, to_tree}; |
| 42 | +use std::rc::Rc; |
| 43 | +use std::cell::RefCell; |
| 44 | + |
| 45 | +// submission codes start here |
| 46 | + |
| 47 | +/* |
| 48 | + 非递归中序遍历 |
| 49 | + */ |
| 50 | +pub struct BSTIterator { |
| 51 | + stack: Vec<Rc<RefCell<TreeNode>>>, |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +/** |
| 56 | + * `&self` means the method takes an immutable reference. |
| 57 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 58 | + */ |
| 59 | +impl BSTIterator { |
| 60 | + |
| 61 | + pub fn new(root: Option<Rc<RefCell<TreeNode>>>) -> Self { |
| 62 | + let mut node = root; |
| 63 | + let mut stack = Vec::new(); |
| 64 | + while let Some(inner) = node.clone() { |
| 65 | + stack.push(inner.clone()); |
| 66 | + node = node.unwrap().borrow().left.clone(); |
| 67 | + } |
| 68 | + BSTIterator{ |
| 69 | + stack: stack, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** @return the next smallest number */ |
| 74 | + pub fn next(&mut self) -> i32 { |
| 75 | + let node = self.stack.pop().unwrap(); |
| 76 | + let res = node.borrow().val; |
| 77 | + let mut next = node.borrow().right.clone(); |
| 78 | + while let Some(inner) = next.clone() { |
| 79 | + self.stack.push(inner.clone()); |
| 80 | + next = next.unwrap().borrow().left.clone(); |
| 81 | + } |
| 82 | + res |
| 83 | + } |
| 84 | + |
| 85 | + /** @return whether we have a next smallest number */ |
| 86 | + pub fn has_next(&self) -> bool { |
| 87 | + !self.stack.is_empty() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Your BSTIterator object will be instantiated and called as such: |
| 93 | + * let obj = BSTIterator::new(root); |
| 94 | + * let ret_1: i32 = obj.next(); |
| 95 | + * let ret_2: bool = obj.has_next(); |
| 96 | + */ |
| 97 | + |
| 98 | +// submission codes end |
| 99 | + |
| 100 | +#[cfg(test)] |
| 101 | +mod tests { |
| 102 | + use super::*; |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_173() { |
| 106 | + let mut iterator = BSTIterator::new(tree![7,3,15,null,null,9,20]); |
| 107 | + assert_eq!(iterator.next(), 3); // return 3 |
| 108 | + assert_eq!(iterator.next(), 7); // return 7 |
| 109 | + assert_eq!(iterator.has_next(), true); // return true |
| 110 | + assert_eq!(iterator.next(), 9); // return 9 |
| 111 | + assert_eq!(iterator.has_next(), true); // return true |
| 112 | + assert_eq!(iterator.next(), 15); // return 15 |
| 113 | + assert_eq!(iterator.has_next(), true); // return true |
| 114 | + assert_eq!(iterator.next(), 20); // return 20 |
| 115 | + assert_eq!(iterator.has_next(), false); // return false |
| 116 | + } |
| 117 | +} |
0 commit comments