概述
典型的遍历模式:前序遍历、中序遍历、逆序遍历
中序遍历(左根右)
可以实现二叉树的按照大小进行打印
public static void printTreeMid(Node root){
//参数传个空的进来,就直接返回掉
if(root==null) return;
printTreeMid(root.left);
System.out.println(root.value);
printTreeMid(root.right);
}
二叉树按照层级遍历
通过一个双向链表,非常巧妙的一种实现模式
private static void cengci(Node root) {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
root = queue.poll();
System.out.print(root.value+"-");
if (root.left != null) {
queue.add(root.left);
}
if (root.right != null) {
queue.add(root.right);
}
}
}
网友评论