public void LevelOrderReverse() {
System.out.println(LevelOrderReverse(this));
}
private String LevelOrderReverse(BinarySearchTree T) {
StringBuffer sb = new StringBuffer();
if(T!=null) {
Queue<BinarySearchTree> Q = new LinkedList<BinarySearchTree>();
Stack<BinarySearchTree> S = new Stack<BinarySearchTree>();
Q.add(T);
while(Q.size() > 0) {
BinarySearchTree C = Q.remove();
if (C.right != null)
Q.add(C.right);
if (C.left != null)
Q.add(C.left);
S.push(C);
}
while(S.size() > 0)
sb.append(S.pop().data).append(", ");
}
return sb.toString();
}
No comments:
Post a Comment