请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次。请从上到下计算出所有折痕的⽅向。
给定折的次数n,请返回从上到下的折痕的数组,若为下折痕则对应元素为"down",若为上折痕则为"up".
测试样例:
1
返回:["down"]
请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次。请从上到下计算出所有折痕的⽅向。
给定折的次数n,请返回从上到下的折痕的数组,若为下折痕则对应元素为"down",若为上折痕则为"up".
1
返回:["down"]
import java.util.*; public class FoldPaper { public String[] foldPaper(int n) { if (n == 0) return new String[0]; // write code here //就是个中序遍历 Queue<Node> queue = new LinkedList(); Node root = new Node("down"); queue.add(root); int height = 1; //这里是在用层序遍历建二叉树 while (true) { int num = queue.size(); while (num > 0) { Node cur = queue.poll(); cur.left = new Node("down"); queue.add(cur.left); cur.right = new Node("up"); queue.add(cur.right); num--; } //高度等于n说明建树完毕 if (++height == n) { break; } } //用逗号分隔得到数组 return inOrder(root).split(","); } static StringBuilder path = new StringBuilder(); 中序遍历拼接结果,用逗号做分割 public String inOrder(Node root) { if (root == null) return ""; inOrder(root.left); path.append(root.val+","); inOrder(root.right); return path.toString(); } } class Node { String val; Node right; Node left; public Node (String val) { this.val = val; } }
import java.util.Scanner; public class Main{ //创建树 public static Node<String> createTree(Node<String> root,int n){ //创建一棵以root为根结点,深度为n的树,返回树的根结点 //如果n=2,则构建左右子树并退出递归 if (n==2) { //创建树的根结点 //Node<String> root = new Node("down", null, null); root.left=new Node("down", null, null); root.right=new Node("up", null, null); return root; }else if(n>2){ //创建树的根结点 //Node<String> root = new Node("down", null, null); //创建树的左子树,并让根结点指向左子树 root.left=new Node("down", null, null); root.right=new Node("up", null, null); root.left = createTree(root.left,n - 1); //创建树的右子树,并让根结点指向右子树 root.right = createTree(root.right,n - 1); return root; } return root; } //打印输出 public static void printTree(Node<String> x) { if (x == null) { return; } if (x.left!=null){ printTree(x.left); } System.out.println(x.item); if (x.right!=null){ printTree(x.right); } } //主方法 public static void main(String[] args) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); Node<String> root=new Node<>("down",null, null);//根结点 Node<String> tree=createTree(root,N); printTree(root); } private static class Node<T> { //存储结点值 private T item; //存储左孩子结点 private Node left; //存储右孩子结点 private Node right; //构造方法 public Node(T item, Node left, Node right) { this.item = item; this.left = left; this.right = right; } } }
递归中序遍历 import java.util.*; public class FoldPaper { public String[] foldPaper(int n) { List<String> list = new LinkedList<String>(); listProcess(1,n,true,list); int len = (2<< n-1) -1; String[] strs = new String[len]; for(int i=0; i<len; i++){ strs[i] = list.get(i); } return strs; } public void listProcess(int i, int n, boolean isDown, List<String> list){ if(i>n){ return; } listProcess(i+1,n,true,list); list.add(isDown?"down":"up"); listProcess(i+1,n,false,list); } }