-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path55_bstToDoublyList.java
49 lines (47 loc) · 1.26 KB
/
55_bstToDoublyList.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
class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}
class DoublyListNode {
int val;
DoublyListNode next, prev;
DoublyListNode(int val) {
this.val = val;
this.next = this.prev = null;
}
}
public class Solution {
/**
* @param root: The root of tree
* @return: the head of doubly list node
*/
public DoublyListNode bstToDoublyList(TreeNode root) {
// Write your code here
List<Integer> list = new ArrayList<Integer>();
travelTree(root,list);
if(list.size()==0)
return null;
DoublyListNode node = new DoublyListNode(list.get(0));
DoublyListNode head = node;
for(int i=1; i<list.size(); i++){
DoublyListNode tmp = new DoublyListNode(list.get(i));
node.next = tmp;
tmp.prev = node;
node = node.next;
}
return head;
}
public void travelTree(TreeNode root,List<Integer> list){
if(root==null)
return;
if(root.left!=null)
travelTree(root.left,list);
list.add(root.val);
if(root.right!=null)
travelTree(root.right,list);
}
}