• 算法数据结构体系学习 第十一节


    实现二叉树的按层遍历

    1)其实就是宽度优先遍历,用队列

    2)可以通过设置flag变量的方式,来发现某一层的结束(看题目)

    public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    
    		public Node(int v) {
    			value = v;
    		}
    	}
    
    	public static void level(Node head) {
    		if (head == null) {
    			return;
    		}
    		Queue<Node> queue = new LinkedList<>();
    		queue.add(head);
    		while (!queue.isEmpty()) {
    			Node cur = queue.poll();
    			System.out.println(cur.value);
    			if (cur.left != null) {
    				queue.add(cur.left);
    			}
    			if (cur.right != null) {
    				queue.add(cur.right);
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		Node head = new Node(1);
    		head.left = new Node(2);
    		head.right = new Node(3);
    		head.left.left = new Node(4);
    		head.left.right = new Node(5);
    		head.right.left = new Node(6);
    		head.right.right = new Node(7);
    
    		level(head);
    		System.out.println("========");
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    实现二叉树的序列化和反序列化

    1)先序方式序列化和反序列化

    2)按层方式序列化和反序列化

        /*
         * 二叉树可以通过先序、后序或者按层遍历的方式序列化和反序列化,
         * 以下代码全部实现了。
         * 但是,二叉树无法通过中序遍历的方式实现序列化和反序列化
         * 因为不同的两棵树,可能得到同样的中序序列,即便补了空位置也可能一样。
         * 比如如下两棵树
         *         __2
         *        /
         *       1
         *       和
         *       1__
         *          \
         *           2
         * 补足空位置的中序遍历结果都是{ null, 1, null, 2, null}
         *       
         * */
    	public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    
    	public static Queue<String> preSerial(Node head) {
    		Queue<String> ans = new LinkedList<>();
    		pres(head, ans);
    		return ans;
    	}
    
    	public static void pres(Node head, Queue<String> ans) {
    		if (head == null) {
    			ans.add(null);
    		} else {
    			ans.add(String.valueOf(head.value));
    			pres(head.left, ans);
    			pres(head.right, ans);
    		}
    	}
    
    	public static Queue<String> inSerial(Node head) {
    		Queue<String> ans = new LinkedList<>();
    		ins(head, ans);
    		return ans;
    	}
    
    	public static void ins(Node head, Queue<String> ans) {
    		if (head == null) {
    			ans.add(null);
    		} else {
    			ins(head.left, ans);
    			ans.add(String.valueOf(head.value));
    			ins(head.right, ans);
    		}
    	}
    
    	public static Queue<String> posSerial(Node head) {
    		Queue<String> ans = new LinkedList<>();
    		poss(head, ans);
    		return ans;
    	}
    
    	public static void poss(Node head, Queue<String> ans) {
    		if (head == null) {
    			ans.add(null);
    		} else {
    			poss(head.left, ans);
    			poss(head.right, ans);
    			ans.add(String.valueOf(head.value));
    		}
    	}
    
    	public static Node buildByPreQueue(Queue<String> prelist) {
    		if (prelist == null || prelist.size() == 0) {
    			return null;
    		}
    		return preb(prelist);
    	}
    
    	public static Node preb(Queue<String> prelist) {
    		String value = prelist.poll();
    		if (value == null) {
    			return null;
    		}
    		Node head = new Node(Integer.valueOf(value));
    		head.left = preb(prelist);
    		head.right = preb(prelist);
    		return head;
    	}
    
    	public static Node buildByPosQueue(Queue<String> poslist) {
    		if (poslist == null || poslist.size() == 0) {
    			return null;
    		}
    		// 左右中  ->  stack(中右左)
    		Stack<String> stack = new Stack<>();
    		while (!poslist.isEmpty()) {
    			stack.push(poslist.poll());
    		}
    		return posb(stack);
    	}
    
    	public static Node posb(Stack<String> posstack) {
    		String value = posstack.pop();
    		if (value == null) {
    			return null;
    		}
    		Node head = new Node(Integer.valueOf(value));
    		head.right = posb(posstack);
    		head.left = posb(posstack);
    		return head;
    	}
    
    	public static Queue<String> levelSerial(Node head) {
    		Queue<String> ans = new LinkedList<>();
    		if (head == null) {
    			ans.add(null);
    		} else {
    			ans.add(String.valueOf(head.value));
    			Queue<Node> queue = new LinkedList<Node>();
    			queue.add(head);
    			while (!queue.isEmpty()) {
    				head = queue.poll(); // head 父   子
    				if (head.left != null) {
    					ans.add(String.valueOf(head.left.value));
    					queue.add(head.left);
    				} else {
    					ans.add(null);
    				}
    				if (head.right != null) {
    					ans.add(String.valueOf(head.right.value));
    					queue.add(head.right);
    				} else {
    					ans.add(null);
    				}
    			}
    		}
    		return ans;
    	}
    
    	public static Node buildByLevelQueue(Queue<String> levelList) {
    		if (levelList == null || levelList.size() == 0) {
    			return null;
    		}
    		Node head = generateNode(levelList.poll());
    		Queue<Node> queue = new LinkedList<Node>();
    		if (head != null) {
    			queue.add(head);
    		}
    		Node node = null;
    		while (!queue.isEmpty()) {
    			node = queue.poll();
    			node.left = generateNode(levelList.poll());
    			node.right = generateNode(levelList.poll());
    			if (node.left != null) {
    				queue.add(node.left);
    			}
    			if (node.right != null) {
    				queue.add(node.right);
    			}
    		}
    		return head;
    	}
    
    	public static Node generateNode(String val) {
    		if (val == null) {
    			return null;
    		}
    		return new Node(Integer.valueOf(val));
    	}
    
    	// for test
    	public static Node generateRandomBST(int maxLevel, int maxValue) {
    		return generate(1, maxLevel, maxValue);
    	}
    
    	// for test
    	public static Node generate(int level, int maxLevel, int maxValue) {
    		if (level > maxLevel || Math.random() < 0.5) {
    			return null;
    		}
    		Node head = new Node((int) (Math.random() * maxValue));
    		head.left = generate(level + 1, maxLevel, maxValue);
    		head.right = generate(level + 1, maxLevel, maxValue);
    		return head;
    	}
    
    	// for test
    	public static boolean isSameValueStructure(Node head1, Node head2) {
    		if (head1 == null && head2 != null) {
    			return false;
    		}
    		if (head1 != null && head2 == null) {
    			return false;
    		}
    		if (head1 == null && head2 == null) {
    			return true;
    		}
    		if (head1.value != head2.value) {
    			return false;
    		}
    		return isSameValueStructure(head1.left, head2.left) && isSameValueStructure(head1.right, head2.right);
    	}
    
    	// for test
    	public static void printTree(Node head) {
    		System.out.println("Binary Tree:");
    		printInOrder(head, 0, "H", 17);
    		System.out.println();
    	}
    
    	public static void printInOrder(Node head, int height, String to, int len) {
    		if (head == null) {
    			return;
    		}
    		printInOrder(head.right, height + 1, "v", len);
    		String val = to + head.value + to;
    		int lenM = val.length();
    		int lenL = (len - lenM) / 2;
    		int lenR = len - lenM - lenL;
    		val = getSpace(lenL) + val + getSpace(lenR);
    		System.out.println(getSpace(height * len) + val);
    		printInOrder(head.left, height + 1, "^", len);
    	}
    
    	public static String getSpace(int num) {
    		String space = " ";
    		StringBuffer buf = new StringBuffer("");
    		for (int i = 0; i < num; i++) {
    			buf.append(space);
    		}
    		return buf.toString();
    	}
    
    	public static void main(String[] args) {
    		int maxLevel = 5;
    		int maxValue = 100;
    		int testTimes = 1000000;
    		System.out.println("test begin");
    		for (int i = 0; i < testTimes; i++) {
    			Node head = generateRandomBST(maxLevel, maxValue);
    			Queue<String> pre = preSerial(head);
    			Queue<String> pos = posSerial(head);
    			Queue<String> level = levelSerial(head);
    			Node preBuild = buildByPreQueue(pre);
    			Node posBuild = buildByPosQueue(pos);
    			Node levelBuild = buildByLevelQueue(level);
    			if (!isSameValueStructure(preBuild, posBuild) || !isSameValueStructure(posBuild, levelBuild)) {
    				System.out.println("Oops!");
    			}
    		}
    		System.out.println("test finish!");
    		
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256

    Encode N-ary Tree to Binary Tree

    // 提交时不要提交这个类
    	public static class Node {
    		public int val;
    		public List<Node> children;
    
    		public Node() {
    		}
    
    		public Node(int _val) {
    			val = _val;
    		}
    
    		public Node(int _val, List<Node> _children) {
    			val = _val;
    			children = _children;
    		}
    	};
    
    	// 提交时不要提交这个类
    	public static class TreeNode {
    		int val;
    		TreeNode left;
    		TreeNode right;
    
    		TreeNode(int x) {
    			val = x;
    		}
    	}
    
    	// 只提交这个类即可
    	class Codec {
    		// Encodes an n-ary tree to a binary tree.
    		public TreeNode encode(Node root) {
    			if (root == null) {
    				return null;
    			}
    			TreeNode head = new TreeNode(root.val);
    			head.left = en(root.children);
    			return head;
    		}
    
    		private TreeNode en(List<Node> children) {
    			TreeNode head = null;
    			TreeNode cur = null;
    			for (Node child : children) {
    				TreeNode tNode = new TreeNode(child.val);
    				if (head == null) {
    					head = tNode;
    				} else {
    					cur.right = tNode;
    				}
    				cur = tNode;
    				cur.left = en(child.children);
    			}
    			return head;
    		}
    
    		// Decodes your binary tree to an n-ary tree.
    		public Node decode(TreeNode root) {
    			if (root == null) {
    				return null;
    			}
    			return new Node(root.val, de(root.left));
    		}
    
    		public List<Node> de(TreeNode root) {
    			List<Node> children = new ArrayList<>();
    			while (root != null) {
    				Node cur = new Node(root.val, de(root.left));
    				children.add(cur);
    				root = root.right;
    			}
    			return children;
    		}
    
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    如何设计一个打印整棵树的打印函数

    	public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    
    	public static void printTree(Node head) {
    		System.out.println("Binary Tree:");
    		printInOrder(head, 0, "H", 17);
    		System.out.println();
    	}
    
    	public static void printInOrder(Node head, int height, String to, int len) {
    		if (head == null) {
    			return;
    		}
    		printInOrder(head.right, height + 1, "v", len);
    		String val = to + head.value + to;
    		int lenM = val.length();
    		int lenL = (len - lenM) / 2;
    		int lenR = len - lenM - lenL;
    		val = getSpace(lenL) + val + getSpace(lenR);
    		System.out.println(getSpace(height * len) + val);
    		printInOrder(head.left, height + 1, "^", len);
    	}
    
    	public static String getSpace(int num) {
    		String space = " ";
    		StringBuffer buf = new StringBuffer("");
    		for (int i = 0; i < num; i++) {
    			buf.append(space);
    		}
    		return buf.toString();
    	}
    
    	public static void main(String[] args) {
    		Node head = new Node(1);
    		head.left = new Node(-222222222);
    		head.right = new Node(3);
    		head.left.left = new Node(Integer.MIN_VALUE);
    		head.right.left = new Node(55555555);
    		head.right.right = new Node(66);
    		head.left.left.right = new Node(777);
    		printTree(head);
    
    		head = new Node(1);
    		head.left = new Node(2);
    		head.right = new Node(3);
    		head.left.left = new Node(4);
    		head.right.left = new Node(5);
    		head.right.right = new Node(6);
    		head.left.left.right = new Node(7);
    		printTree(head);
    
    		head = new Node(1);
    		head.left = new Node(1);
    		head.right = new Node(1);
    		head.left.left = new Node(1);
    		head.right.left = new Node(1);
    		head.right.right = new Node(1);
    		head.left.left.right = new Node(1);
    		printTree(head);
    
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    求二叉树最宽的层有多少个节点

    二叉树结构如下定义:
    Class Node {
    V value;
    Node left;
    Node right;
    Node parent;
    }

    	public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    
    	public static int maxWidthUseMap(Node head) {
    		if (head == null) {
    			return 0;
    		}
    		Queue<Node> queue = new LinkedList<>();
    		queue.add(head);
    		// key 在 哪一层,value
    		HashMap<Node, Integer> levelMap = new HashMap<>();
    		levelMap.put(head, 1);
    		int curLevel = 1; // 当前你正在统计哪一层的宽度
    		int curLevelNodes = 0; // 当前层curLevel层,宽度目前是多少
    		int max = 0;
    		while (!queue.isEmpty()) {
    			Node cur = queue.poll();
    			int curNodeLevel = levelMap.get(cur);
    			if (cur.left != null) {
    				levelMap.put(cur.left, curNodeLevel + 1);
    				queue.add(cur.left);
    			}
    			if (cur.right != null) {
    				levelMap.put(cur.right, curNodeLevel + 1);
    				queue.add(cur.right);
    			}
    			if (curNodeLevel == curLevel) {
    				curLevelNodes++;
    			} else {
    				max = Math.max(max, curLevelNodes);
    				curLevel++;
    				curLevelNodes = 1;
    			}
    		}
    		max = Math.max(max, curLevelNodes);
    		return max;
    	}
    
    	public static int maxWidthNoMap(Node head) {
    		if (head == null) {
    			return 0;
    		}
    		Queue<Node> queue = new LinkedList<>();
    		queue.add(head);
    		Node curEnd = head; // 当前层,最右节点是谁
    		Node nextEnd = null; // 下一层,最右节点是谁
    		int max = 0;
    		int curLevelNodes = 0; // 当前层的节点数
    		while (!queue.isEmpty()) {
    			Node cur = queue.poll();
    			if (cur.left != null) {
    				queue.add(cur.left);
    				nextEnd = cur.left;
    			}
    			if (cur.right != null) {
    				queue.add(cur.right);
    				nextEnd = cur.right;
    			}
    			curLevelNodes++;
    			if (cur == curEnd) {
    				max = Math.max(max, curLevelNodes);
    				curLevelNodes = 0;
    				curEnd = nextEnd;
    			}
    		}
    		return max;
    	}
    
    	// for test
    	public static Node generateRandomBST(int maxLevel, int maxValue) {
    		return generate(1, maxLevel, maxValue);
    	}
    
    	// for test
    	public static Node generate(int level, int maxLevel, int maxValue) {
    		if (level > maxLevel || Math.random() < 0.5) {
    			return null;
    		}
    		Node head = new Node((int) (Math.random() * maxValue));
    		head.left = generate(level + 1, maxLevel, maxValue);
    		head.right = generate(level + 1, maxLevel, maxValue);
    		return head;
    	}
    
    	public static void main(String[] args) {
    		int maxLevel = 10;
    		int maxValue = 100;
    		int testTimes = 1000000;
    		for (int i = 0; i < testTimes; i++) {
    			Node head = generateRandomBST(maxLevel, maxValue);
    			if (maxWidthUseMap(head) != maxWidthNoMap(head)) {
    				System.out.println("Oops!");
    			}
    		}
    		System.out.println("finish!");
    
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    给你二叉树中的某个节点,返回该节点的后继节点

    限定中序遍历

    public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    		public Node parent;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    
    	public static Node getSuccessorNode(Node node) {
    		if (node == null) {
    			return node;
    		}
    		if (node.right != null) {
    			return getLeftMost(node.right);
    		} else { // 无右子树
    			Node parent = node.parent;
    			while (parent != null && parent.right == node) { // 当前节点是其父亲节点右孩子
    				node = parent;
    				parent = node.parent;
    			}
    			return parent;
    		}
    	}
    
    	public static Node getLeftMost(Node node) {
    		if (node == null) {
    			return node;
    		}
    		while (node.left != null) {
    			node = node.left;
    		}
    		return node;
    	}
    
    	public static void main(String[] args) {
    		Node head = new Node(6);
    		head.parent = null;
    		head.left = new Node(3);
    		head.left.parent = head;
    		head.left.left = new Node(1);
    		head.left.left.parent = head.left;
    		head.left.left.right = new Node(2);
    		head.left.left.right.parent = head.left.left;
    		head.left.right = new Node(4);
    		head.left.right.parent = head.left;
    		head.left.right.right = new Node(5);
    		head.left.right.right.parent = head.left.right;
    		head.right = new Node(9);
    		head.right.parent = head;
    		head.right.left = new Node(8);
    		head.right.left.parent = head.right;
    		head.right.left.left = new Node(7);
    		head.right.left.left.parent = head.right.left;
    		head.right.right = new Node(10);
    		head.right.right.parent = head.right;
    
    		Node test = head.left.left;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.left.left.right;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.left;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.left.right;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.left.right.right;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.right.left.left;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.right.left;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.right;
    		System.out.println(test.value + " next: " + getSuccessorNode(test).value);
    		test = head.right.right; // 10's next is null
    		System.out.println(test.value + " next: " + getSuccessorNode(test));
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    请把一段纸条竖着放在桌子上,然后从纸条的下边向上方对折1次,压出折痕后展开。此时折痕是凹下去的,即折痕突起的方向指向纸条的背面。 如果从纸条的下边向上方连续对折2次,压出折痕后展开,此时有三条折痕,从上到下依次是下折痕、下折痕和上折痕。 给定一个输入参数N,代表纸条都从下边向上方连续对折N次。 请从上到下打印所有折痕的方向。 例如:N=1时,打印: down N=2时,打印: down down up

    	public static void printAllFolds(int N) {
    		process(1, N, true);
    		System.out.println();
    	}
    
    	// 当前你来了一个节点,脑海中想象的!
    	// 这个节点在第i层,一共有N层,N固定不变的
    	// 这个节点如果是凹的话,down = T
    	// 这个节点如果是凸的话,down = F
    	// 函数的功能:中序打印以你想象的节点为头的整棵树!
    	public static void process(int i, int N, boolean down) {
    		if (i > N) {
    			return;
    		}
    		process(i + 1, N, true);
    		System.out.print(down ? "凹 " : "凸 ");
    		process(i + 1, N, false);
    	}
    
    	public static void main(String[] args) {
    		int N = 4;
    		printAllFolds(N);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    八股文--->并发编程
    MATLAB RF随机森林 笔记整理
    FPGA设计时序约束四、多周期约束
    【电路笔记】-晶体管作为开关
    供应链计划 SCP - Supply Chain Planning
    flutter sdk提供完整页面的ui
    【数据结构初阶】二叉树——堆的应用(堆排序 + TOP-K问题)
    什么情况?周鸿祎和三六零违约,“零元”甩卖哪吒汽车10亿元股权
    css列表
    完美PDF打印:PDFPrinting.NET Crack
  • 原文地址:https://blog.csdn.net/qq_29374433/article/details/126325451