public class TreeNode(){
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(){}
重写toString()方法。
}
①前序遍历:根,左,右。
public void preOrder(TreeNode tree){
if(tree==null) return;
printf(tree.val);
preOrder(tree.left);
preOrder(tree.right);
}
②中序遍历:左,根,右
public void inOrder(TreeNode tree){
if(tree==null) return;
inOrder(tree.left);
System.out.printf(tree.val);
inOrder(tree.right);
}
③后序遍历:左,右,根
public static void postOrder(TreeNode tree) {
if (tree == null)
return;
postOrder(tree.left);
postOrder(tree.right);
System.out.println(tree.val);
}
④链表的逆序打印
public void printRevers(ListNode root) {
//(终止条件)
if (root == null)
return;
//(递归调用)先打印下一个
printRevers(root.next);
//(逻辑处理)把后面的都打印完了在打印当前节点
System.out.println(root.val);
}