-
Notifications
You must be signed in to change notification settings - Fork 58
/
Day12.java
211 lines (171 loc) · 3.63 KB
/
Day12.java
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
package com.offer;
public class Day12 {
public static void main(String[] args) {
Day12 d = new Day12();
// int[] push = {
// 1,2,3,4,5
// };
//
// int[] pop = {
// 4,3,2,5,1
// };
//
// boolean result = d.isPopOrder(push, pop);
// System.out.println(result);
BTreeNode head = d.new BTreeNode(0);
BTreeNode node1 = d.new BTreeNode(1);
BTreeNode node2 = d.new BTreeNode(2);
BTreeNode node3 = d.new BTreeNode(3);
BTreeNode node4 = d.new BTreeNode(4);
BTreeNode node5 = d.new BTreeNode(5);
BTreeNode node6 = d.new BTreeNode(6);
BTreeNode node7 = d.new BTreeNode(7);
head.left = node2;
head.right = node1;
node1.left = node4;
node1.right = node6;
node2.left = node3;
node2.right = node5;
node4.left = node7;
d.printTreeInOrder(head);
}
/**
* 判断一个序列是否为 某一个栈的出栈序列
* @param push
* @param pop
* @return
*/
public boolean isPopOrder(int[] push, int[] pop) {
if (push.length == 0 || pop.length == 0 || push.length != pop.length)
return false;
Node head = new Node(0);
int popIndex = 0;
for (int p : push) {
while (head.next != null) {
Node node = head.next;
if (node.data == pop[popIndex]) {
popIndex++;
head.next = node.next;
} else {
break;
}
}
if (p != pop[popIndex]) {
Node node = new Node(p);
node.next = head.next;
head.next = node;
} else {
popIndex++;
}
}
Node node = head.next;
while (node != null) {
if (node.data != pop[popIndex]) {
return false;
} else {
popIndex++;
node = node.next;
}
}
return true;
}
class Node{
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
}
//////////////////////////////////////////////////////////////////
/**
* 二叉树节点
* @author kexun
*
*/
class BTreeNode {
public int data;
public BTreeNode left;
public BTreeNode right;
public BTreeNode(int data) {
this.data = data;
}
}
/**
* 定义一个简单的队列
* @author kexun
*
* @param <T>
*/
class Queue<T> {
/**
* 队列的头指针
*/
public QNode<T> head;
/**
* 队列的尾指针
*/
public QNode<T> last;
/**
* 队列的容器,保存节点数据
*/
class QNode<T> {
public T data;
public QNode<T> next;
public QNode(T data) {
this.data = data;
}
}
public Queue() {
this.head = new QNode<T>(null);
this.last = new QNode<T>(null);
}
/**
* 尾部插入
* @param data
*/
public void addLast(T data) {
if (data != null) {
QNode<T> node = new QNode<T>(data);
if (head.next == null && last.next == null) {
head.next = last.next = node;
} else {
QNode<T> tempNode = last.next;
tempNode.next = node;
last.next = node;
}
}
}
/**
* 头部弹出
* @return
*/
public T pop() {
if (head.next != null) {
QNode<T> node = head.next;
head.next = node.next;
if (head.next == null)
last.next = head.next;
return node.data;
}
return null;
}
}
/**
* 输入树的第一个节点 将一个树从头至尾,从左往右,一层一层打印
* @param node
*/
public void printTreeInOrder(BTreeNode head) {
//维护树节点打印的 辅助队列
Queue<BTreeNode> queue = new Queue<BTreeNode>();
queue.addLast(head);
BTreeNode currNode = queue.pop();
while (currNode != null) {
System.out.println(currNode.data);
if (currNode.left != null)
queue.addLast(currNode.left);
if (currNode.right != null)
queue.addLast(currNode.right);
currNode = queue.pop();
}
}
}