Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit324948c

Browse files
author
applewjg
committed
update
Change-Id: Ia2dee999b1df05f6a6f36a10cf2beeaf1f65889f
1 parent04022c7 commit324948c

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

‎BinarySearchTreeIterator.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Calling next() will return the next smallest number in the BST.
2323
* }
2424
*/
2525

26-
publicclassBSTIterator {
26+
publicclassBSTIterator_1 {
2727

2828
publicBSTIterator(TreeNoderoot) {
2929
stk =newStack<TreeNode>();
@@ -53,6 +53,46 @@ public int next() {
5353
privateTreeNodenode;
5454
}
5555

56+
57+
publicclassBSTIterator_2 {
58+
59+
publicBSTIterator(TreeNoderoot) {
60+
node =root;
61+
}
62+
63+
/** @return whether we have a next smallest number */
64+
publicbooleanhasNext() {
65+
returnnode !=null;
66+
}
67+
68+
/** @return the next smallest number */
69+
publicintnext() {
70+
if (node ==null)return0;
71+
intres =0;
72+
while (node !=null) {
73+
if (node.left ==null) {
74+
res =node.val;
75+
node =node.right;
76+
returnres;
77+
}
78+
TreeNodepre =node.left;
79+
while (pre.right !=null &&pre.right !=node)
80+
pre =pre.right;
81+
if (pre.right ==null) {
82+
pre.right =node;
83+
node =node.left;
84+
}else {
85+
res =node.val;
86+
node =node.right;
87+
pre.right =null;
88+
returnres;
89+
}
90+
}
91+
returnres;
92+
}
93+
privateTreeNodenode;
94+
}
95+
5696
/**
5797
* Your BSTIterator will be called like this:
5898
* BSTIterator i = new BSTIterator(root);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp