Hello everybody,
I've to create a postorder method for a tree (without using java classes like "tree" or similar).
The tree is made in this way:
public class MyTree
{
private BinNode root;
private class BinNode{
public BinNode left;
public BinNode right;
public int label;
}
}
The real problem I have is that my method is NOT void, so I can't simply do:
if(left!=null) return left.toStringIntern();
if(right!=null) return right.toStringIntern();
System.out.println(label);
because, at the end of the code, I need to return a value. I think this is the problem, because if I see just the last node of the tree..probably after one System.out I return the value and so I can't see the other nodes..but how can I fix it?
I'll post you the code:
public String toString(){
if(root!=null){
return root.toStringIntern();
}
else
return "";
}
public String toStringIntern(){
if(left!=null) return left.toStringIntern();
if(right!=null) return right.toStringIntern();
System.out.println(label);
return ""; //I'll need to return a string later..but it's complicated because I've to create a string like [_,1,[[_,3,_],2,_]]
}
null
Message was edited by:
Pipkin