其他分享
首页 > 其他分享> > 模拟递归(二叉树中序遍历)

模拟递归(二叉树中序遍历)

作者:互联网

一道在国外很流行的题目。题目:请把一段纸条竖着放在桌子上,然后从纸条的下边向上方对折一次,压出折痕后展开。此时折痕是凹下去的。如果从纸条的下边向上方连续对折两次,压出折痕后展开,此时有三条折痕,从上到下依次是下折痕,下折痕和上折痕。(每次折完后,该折痕上边的折痕都是凹的,该折痕下边的折痕都是凸的,左子树都是凹的,右子树都是凸的)
给定一个输入参数N,代表纸条都从下边向上方连续对折N次。请从上到下打印所有折痕的方向。
例如:N=1时,打印凹,N=2时,打印凹凹凸

public class PaperFolding{
    public staticvoid printAllFolds(int N){
        prin
    }

    //递归过程,来到了某一个节点,
    //i是节点的层数,N一共的层数,down == true 凹 down == false 凸
    public static void printProcess(int i,int N,boolean down){
        if(i > N){
            return;
        }
        printProcess(i + 1, N ,true);
        System.out.println(down ? "凹" : "凸");
        printProcess(i + 1, N  ,false);
    }

    public static void main(String[] args) {
        int N = 3;
        printAllFolds(N);
    }
}

其实就是二叉树的中序遍历

标签:printProcess,遍历,int,中序,纸条,down,二叉树,public,折痕
来源: https://blog.csdn.net/Niall_/article/details/122682504