-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain36.java
50 lines (44 loc) · 1.11 KB
/
Main36.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
package JZOffer2;
import java.util.Deque;
import java.util.LinkedList;
public class Main36 {
public DoubleNode treeToDoublyList(DoubleNode root) {
if(root == null){
return null;
}
Deque<DoubleNode> stk = new LinkedList<DoubleNode>();
DoubleNode pre= null, head = null;
while(root != null || ! stk.isEmpty()){
while(root != null){
stk.push(root);
root = root.left;
}
root = stk.pop();
root.left = pre;
if(pre != null){
pre.right = root;
}else{
head = root;
}
pre = root;
root = root.right;
}
head.left = pre;
pre.right = head;
return head;
}
}
class DoubleNode {
public int val;
public DoubleNode left;
public DoubleNode right;
public DoubleNode() {}
public DoubleNode(int _val) {
val = _val;
}
public DoubleNode(int _val,DoubleNode _left,DoubleNode _right) {
val = _val;
left = _left;
right = _right;
}
};