forked from striver79/FreeKaTreeSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
public class BSTIterator { | ||
private Stack<TreeNode> stack = new Stack<TreeNode>(); | ||
boolean reverse = true; | ||
|
||
public BSTIterator(TreeNode root, boolean isReverse) { | ||
reverse = isReverse; | ||
pushAll(root); | ||
} | ||
|
||
/** @return whether we have a next smallest number */ | ||
public boolean hasNext() { | ||
return !stack.isEmpty(); | ||
} | ||
|
||
/** @return the next smallest number */ | ||
public int next() { | ||
TreeNode tmpNode = stack.pop(); | ||
if(reverse == false) pushAll(tmpNode.right); | ||
else pushAll(tmpNode.left); | ||
return tmpNode.val; | ||
} | ||
|
||
private void pushAll(TreeNode node) { | ||
while(node != null) { | ||
stack.push(node); | ||
if(reverse == true) { | ||
node = node.right; | ||
} else { | ||
node = node.left; | ||
} | ||
} | ||
} | ||
} | ||
class Solution { | ||
public boolean findTarget(TreeNode root, int k) { | ||
if(root == null) return false; | ||
BSTIterator l = new BSTIterator(root, false); | ||
BSTIterator r = new BSTIterator(root, true); | ||
|
||
int i = l.next(); | ||
int j = r.next(); | ||
while(i<j) { | ||
if(i + j == k) return true; | ||
else if(i + j < k) i = l.next(); | ||
else j = r.next(); | ||
} | ||
return false; | ||
} | ||
} |