1.二叉树节点结构
class Node<V>{
V value;
Node left;
Node right;
}
用递归和非递归两种方式实现二叉树的先序、中序、后序遍历
如果直观的打印一颗二叉树
如何完成二叉树的宽度优先遍历(常见题目:求一颗二叉树的宽度)
# 二叉树先序遍历
public static void preOrderRecur(Node head){
if(head == null){
return;
}
System.out.print(head.value+" ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
# 二叉树中序遍历
public static void inOrderRecur(Node head){
if(head == null){
return;
}
preOrderRecur(head.left);
System.out.print(head.value+" ");
preOrderRecur(head.right);
}
# 二叉树后序遍历
public static void inOrderRecur(Node head){
if(head == null){
return;
}
preOrderRecur(head.left);
preOrderRecur(head.right);
System.out.print(head.value+" ");
}
网友评论