###### leetcode 173. Binary Search Tree Iterator-二叉搜索树迭代|中序遍历

【思路-Java】中序遍历非递归实现

public class BSTIterator {
Stack<TreeNode> stack = new Stack<TreeNode>();
public BSTIterator(TreeNode root) {
while(root != null) {
root = root.left;
}
}

/** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty();
}

/** @return the next smallest number */
public int next() {
TreeNode temp = stack.pop();
int val = temp.val;
if(temp.right != null) {
temp = temp.right;
while (temp != null) {
temp = temp.left;
}
}
return val;
}
}
61 / 61 test cases passed. Runtime: 6 ms  Your runtime beats 78.97% of javasubmissions.

【思路2-Java】

public class BSTIterator {
TreeNode root = new TreeNode(0);
public BSTIterator(TreeNode root) {
this.root.right = root;
}

/** @return whether we have a next smallest number */
public boolean hasNext() {
return root.right != null;
}

/** @return the next smallest number */
public int next() {
TreeNode next = root.right;
if (next.left == null) {
root = next;
return next.val;
}
TreeNode right = root.right;
TreeNode parent = root;
while(next.left != null) {
parent = next;
next = next.left;
}
parent.left = next.right;
root.right = next;
next.right = right;
root = root.right;
return root.val;
}
}
61 / 61 test cases passed. Runtime: 5 ms  Your runtime beats 94.32% of javasubmissions.

#### 搜索二叉树含迭代器

2017-06-08 21:30:23

#### 二叉树遍历（递归与迭代）

2013-06-09 18:47:57

#### LeetCode(173) Binary Tree Iterator

2015-01-02 15:43:47

#### leetcode-173：Binary Search Tree Iterator（Java）

2015-08-18 10:58:59

#### leetcode 173: Binary Search Tree Iterator

2015-01-02 12:44:56

#### [LeetCode 173] Binary Search Tree Iterator

2015-08-21 14:11:58

#### 实现二叉树中序Iterator

2014-11-19 01:34:01

#### 二叉搜索树的后序遍历序列（Java），复杂度O（n）解法

2016-09-10 10:52:37

#### Binary Search Tree Iterator leetcode

2016-08-17 23:06:55

#### LintCode_二叉查找树迭代器

2017-08-23 21:21:12

## 不良信息举报

leetcode 173. Binary Search Tree Iterator-二叉搜索树迭代|中序遍历