zoukankan      html  css  js  c++  java
  • 纸条从下往上对折N次,打印每次折痕的方向


    /**
    * 纸条从下往上对折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;
    }

    }

    }

    /* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */
  • 相关阅读:
    BZOJ5212 ZJOI2018历史(LCT)
    BZOJ5127 数据校验
    253. Meeting Rooms II
    311. Sparse Matrix Multiplication
    254. Factor Combinations
    250. Count Univalue Subtrees
    259. 3Sum Smaller
    156. Binary Tree Upside Down
    360. Sort Transformed Array
    348. Design Tic-Tac-Toe
  • 原文地址:https://www.cnblogs.com/laydown/p/12952749.html
Copyright © 2011-2022 走看看