/**
* 纸条从下往上对折N次,打印每次折痕的方向
* <p>
* 通过实践得知:纸条对折N次得到的就是一个N层的满二叉树,且头节点是凹折痕,往下的任一子树的左节点都是凹折痕,右节点都是凸折痕
*/
public class PaperFolding {
/**
* 打印折痕方向
*
* @param n 折叠次数
*/
public static void printPaperFolding(int n) {
printAllFolds(1, n, true);
}
/**
* 递归打印折痕方向
*
* @param i 第几层
* @param n 折叠总次数
* @param down 是否向下(凹折痕)
*/
private static void printAllFolds(int i, int n, boolean down) {
if (i > n) {
return;
}
printAllFolds(i + 1, n, true);
System.out.println(down ? "凹" : "凸");
printAllFolds(i + 1, n, false);
}
public static void main(String[] args) {
printPaperFolding(3);
}
/**
* 二叉树结构
*/
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int value) {
this.value = value;
}
}
}
/* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */
纸条从下往上对折N次,打印每次折痕的方向
猜你喜欢
转载自www.cnblogs.com/laydown/p/12952749.html
今日推荐
周排行