origami problem
【topic】
Please put a piece of paper upright on the table, then fold it in half from the bottom edge of the paper to the top, press the crease and unfold it. at this time
The crease is concave, ie the direction of the crease protrusion points towards the back of the strip. If the paper strip is folded in half continuously from the bottom edge to the top
Next, press out the crease and then unfold, there are three crease at this time, from top to bottom are the lower crease, the lower crease and the upper crease. given a
The input parameter N means that the paper strips are folded N times continuously from the bottom to the top. Please print the directions of all creases from top to bottom.
For example: When N=1, print:
down
When N=2, print:
down
down
up
ideas
It can be seen that this is a full binary tree structure, and the in-order traversal of the structure is the desired
code
public class PaperFolding { public static void printAllFolds(int N) { printProcess(1, N, true); } //i represents the current binary tree level public static void printProcess(int i, int N, boolean down) { if (i > N) { return; } printProcess(i + 1, N, true); System.out.println(down ? "down " : "up "); printProcess(i + 1, N, false); } public static void main(String[] args) { int N = 3; printAllFolds(N); } }