递归:
public void preOrderRecur(Node root) { if (root == null) { return; } System.out.print(root.data + " -> "); preOrderRecur(root.left); preOrderRecur(root.right); }
非递归:
public void preOrder() { if (root == null) return; Node current; //把LinkedList当栈使用 LinkedList<Node> s = new LinkedList<Node>(); s.addFirst(root); while (!s.isEmpty()) { current = s.removeFirst(); System.out.print(current.data + " -> "); if (current.right != null) s.addFirst(current.right); if (current.left != null) s.addFirst(current.left); } }