镜像站点
· 本页由第三方 GitHub 只读镜像提供,
非 GitHub 官方站点
,不接受任何登录或凭据输入。
前往 github.com
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
LinXin04
LeetCode-Java
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode-Java
/
BinarySearchTreeIterator.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
62 lines (58 loc) · 1.62 KB
master
Breadcrumbs
LeetCode-Java
/
BinarySearchTreeIterator.java
Copy path
Top
File metadata and controls
Code
Blame
62 lines (58 loc) · 1.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* 题目:二叉搜索树迭代器。完成两个功能:判断是否存在下一个最小的数值、返回下一个更小的数值。要求空间复杂度为O(logn)
* 解题思路:
* 二叉搜索树的最大特点:中序遍历的结果为升序的。
* 对于第一个问题,我们可以借助一个栈来存储所有的可能最小的点。
* 对于第二个问题,我们需要考虑如果第一次弹出最小的元素的话,我们可以成功。
* 如:
* 6
/ \
5 8
/ / \
1 7 9
\
3
/ \
2 4
* 首先s=6,5,1,第一次1弹出,如果不对栈进行维护的话,再调用一次next的时候就会弹出5,这是不对的。
* 这时应该弹出2, 所以我们应该将元素1的右子树的部分节点加入到栈中,但是我们不需要加入所有的节点,而是将根到左下角的点加入即可,即3,2
*
*/
import java.util.Stack;
public class BinarySearchTreeIterator {
}
class BSTIterator
{
Stack<TreeNode> s=new Stack<TreeNode>();
public BSTIterator(TreeNode root)
{
while(root!=null)
{
s.push(root);
root=root.left;
}
}
//判断是否存在下一个最小的数值并返回
public boolean hasNext()
{
return !s.empty();
}
//返回下一个最小的数值
public int next()
{
//首先将最小元素取出来
TreeNode resNode=s.pop();
int result=resNode.val;
//维护栈。判断这个resNode的右子树是否为空。
if(resNode.right!=null)
{
resNode=resNode.right;
while(resNode!=null)
{
s.push(resNode);
resNode=resNode.left;
}
}
return result;
}
}
You can’t perform that action at this time.