-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
binarySearchTreeIterator.java
70 lines (61 loc) · 2.08 KB
/
binarySearchTreeIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Source : https://oj.leetcode.com/problems/balanced-binary-tree/
// Inspired by : http://www.jiuzhang.com/solutions/binary-search-tree-iterator/
// Author : Lei Cao
// Date : 2015-10-07
/**********************************************************************************
*
* Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
*
* Calling next() will return the next smallest number in the BST.
*
* Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
*
* Credits:Special thanks to @ts for adding this problem and creating all test cases.
*
**********************************************************************************/
package binarySearchTreeIterator;
import java.util.Stack;
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
* Example of iterate a tree:
* Solution iterator = new Solution(root);
* while (iterator.hasNext()) {
* TreeNode node = iterator.next();
* do something for node
* }
*/
public class binarySearchTreeIterator {
private TreeNode currentNode = null;
private Stack<TreeNode> stack = new Stack<TreeNode>();
//@param root: The root of binary tree.
public binarySearchTreeIterator(TreeNode root) {
if (root != null) {
currentNode = root;
}
}
//@return: True if there has next node, or false
public boolean hasNext() {
// write your code here
return currentNode != null || !stack.isEmpty();
}
//@return: return next node
public TreeNode next() {
// write your code here
while (currentNode != null) {
stack.push(currentNode);
currentNode = currentNode.left;
}
currentNode = stack.pop();
TreeNode node = currentNode;
currentNode = currentNode.right;
return node;
}
}