My goal is to traverse a Binary Tree and find all values between 2 given values. I am trying to think of a way to go to the lowest point, than traverse the tree in order from left to right. But my code doesn't have pointers to parent nodes so that is out of the question. Is there a way to do this so that I can traverse the tree from left to right?
-
Without seeing your tree implementation code and what you have tried, nobody here can answer your question. Please read the FAQ and How to Ask for tips on writing good questions.Jim Garrison– Jim Garrison2013-06-12 01:32:00 +00:00Commented Jun 12, 2013 at 1:32
-
No worries big guy, BevynQ answered it just fine. If you can't understand text than you will have trouble answering anything at all.Gasper Gulotta– Gasper Gulotta2013-06-12 01:46:01 +00:00Commented Jun 12, 2013 at 1:46
-
No need to be rude. Jim makes a good point.BevynQ– BevynQ2013-06-12 01:48:36 +00:00Commented Jun 12, 2013 at 1:48
-
Every time I try and ask a question on this site people get upset. I've read the articles I'm supposed to read. This was a question about a generic data structure and design, not code. There are passive aggressive geeks sitting behind their computer waiting to rip a beginner any chance they can get. That's not encouraging to the person seeking help nor the field itself.Gasper Gulotta– Gasper Gulotta2013-06-12 01:53:06 +00:00Commented Jun 12, 2013 at 1:53
Add a comment
|
1 Answer
You do not need a pointer to the parent node. The callstack can proxy it, use a recursive method call.
public void traverse(TreeNode node){
if(node == null){
return;
}else {
// display values to the left of current node
traverse(node.left);
// display current node
System.out.println(node.value);
// display values to the right of current node
traverse(node.right);
}
}