0

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?

4
  • 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. Commented 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. Commented Jun 12, 2013 at 1:46
  • No need to be rude. Jim makes a good point. Commented 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. Commented Jun 12, 2013 at 1:53

1 Answer 1

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.