以前数据结构用c写过,被面试问道,尴尬卡住,重写一遍
节点
public class TreeNode<E> {
protected E Element;
protected TreeNode<E>leftChild;
protected TreeNode<E>rightChild;
public TreeNode(E element) {
Element = element;
}
}
递归版
public class BST {
public static void preOrder(TreeNode node) {
if(node==null) return;
System.out.print(node.Element.toString()+" ,");
preOrder(node.leftChild);
preOrder(node.rightChild);
}
public static void inOrder(TreeNode node) {
if(node==null) return;
inOrder(node.leftChild);
System.out.print(node.Element.toString()+" ,");
inOrder(node.rightChild);
}
public static void postOrder(TreeNode node) {
if(node==null) return;
postOrder(node.leftChild);
postOrder(node.rightChild);
System.out.print(node.Element.toString()+" ,");
}
}
非递归版
前序比较简单应为始终向下遍历的,中序 后序 要控制向上还是向下,以下代码中序,后序提供了两种思路
public class NoRecurSiveTreeTraversal {
public static ArrayList preOder(TreeNode node) {
ArrayList lst=new ArrayList();
LinkedList<TreeNode> que=new LinkedList();
que.add(node);
while (!que.isEmpty()) {
TreeNode tmp = que.removeFirst();
lst.add(tmp.Element);
if(tmp.rightChild!=null) que.addFirst(tmp.rightChild);
if(tmp.leftChild!=null) que.addFirst(tmp.leftChild);
}
return lst;
}
public static ArrayList inOder(TreeNode node) {
ArrayList lst=new ArrayList();
if(node==null) return lst;
LinkedList<TreeNode> que=new LinkedList();
TreeNode tmp=node;
while (!que.isEmpty()||tmp!=null) {
if (tmp != null) {
que.addFirst(tmp);
tmp=tmp.leftChild;
}
else {
tmp=que.removeFirst();
lst.add(tmp.Element);
tmp=tmp.rightChild;
}
}
return lst;
}
/* 中序
这里为啥不先判断有子没有,后入栈呢,因为tmp的有无,标志树的向上还是向下。
如果有左子,则入栈,无则出,入右,这么一来 如果连续两层以上左子入栈,那么出栈一次后继续就无法判断向上还是向下
while (!que.isEmpty()||tmp!=null) !=null 控制初始和结束时 比如根出栈时栈空,右子为tmp 还需要处理*/
public static ArrayList postOder(TreeNode node) {
ArrayList lst=new ArrayList();
if(node==null) return lst;
LinkedList<TreeNode> que=new LinkedList();
LinkedList<Boolean> undownFlag=new LinkedList<>();
que.add(node);
undownFlag.add(true);
TreeNode tmp;
while (!que.isEmpty()) {
if (undownFlag.removeFirst()) {
undownFlag.addFirst(false);
tmp=que.getFirst();
if(tmp.rightChild!=null) {
que.addFirst(tmp.rightChild);
undownFlag.addFirst(true);
}
if (tmp.leftChild!=null) {
que.addFirst(tmp.leftChild);
undownFlag.addFirst(true);
}
}
else {
tmp=que.removeFirst();
lst.add(tmp.Element);
}
}
return lst;
}
}
测试
public class test {
public static void main(String[] args) {
/*
简单示例,代码通过了letcode的问题,
1
21 22
x 31 x x
x 41 x
*/
TreeNode root=new TreeNode(1);
root.leftChild=new TreeNode(21);
root.rightChild=new TreeNode(22);
root.leftChild.rightChild=new TreeNode(31);
root.leftChild.rightChild.leftChild=new TreeNode(41);
System.out.println("preorder");
BST.preOrder(root);
System.out.println();
System.out.println(NoRecurSiveTreeTraversal.preOder(root));
System.out.println("inorder");
BST.inOrder(root);
System.out.println();
System.out.println(NoRecurSiveTreeTraversal.inOder(root));
System.out.println("postorder");
BST.postOrder(root);
System.out.println();
System.out.println(NoRecurSiveTreeTraversal.postOder(root));
}
}