-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0173-Binary-search-tree-iterator.cs
More file actions
52 lines (43 loc) · 1.18 KB
/
0173-Binary-search-tree-iterator.cs
File metadata and controls
52 lines (43 loc) · 1.18 KB
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
using Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0173.Binary_search_tree_iterator
{
public class _0173_Binary_search_tree_iterator
{
public class BSTIterator
{
private Stack<TreeNode> stack;
public BSTIterator(TreeNode root)
{
stack = new Stack<TreeNode>();
TraversalLeft(root);
}
public int Next()
{
var next = stack.Pop();
TraversalLeft(next.right);
return next.val;
}
public bool HasNext()
{
return stack.Count > 0;
}
private void TraversalLeft(TreeNode node)
{
while (node != null)
{
stack.Push(node);
node = node.left;
}
}
}
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator obj = new BSTIterator(root);
* int param_1 = obj.Next();
* bool param_2 = obj.HasNext();
*/
}
}