思路介绍
折两次有三次折痕,分别是凹凹凸。
折三次后,凹凹凸凹凹凸凸。
连续折叠n次,依次输出折纸的凹凸。
考察二叉树基础:
即二叉树的中序遍历,遍历我们脑海中的二叉树
这棵树的特点:
1、根节点是凹折痕
2、左树是以凹折痕为头,右树以凸折痕为头
随堂练习
代码:
import java.io.*;
import java.util.*;
public class Main{
//StringBuilder sb=new StringBuilder();
public static void main(String[] args) throws IOException{
//Scanner sc=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
printTree(n);
}
private static void printTree(int n) {
InderOrderPrint(1,n,true);
}
/*
* true代表凹(down),false代表凸(up)
* 中序遍历我们脑海种的树(根节点是凹,左树根节点是凹,右树根节点是凸)
* */
private static void InderOrderPrint(int i, int n, boolean b) {
if(i >n){
return;
}
InderOrderPrint(i+1,n,true);
System.out.println(b ? "down" : "up");
InderOrderPrint(i+1,n,false);
}
}