-
Notifications
You must be signed in to change notification settings - Fork 2
/
BSTtoCircularDLL.java
122 lines (103 loc) · 3.15 KB
/
BSTtoCircularDLL.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
package Leetcode;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* @author kalpak
*
* Convert a Binary Search Tree to a sorted Circular Doubly-Linked List in place.
*
* You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list.
* For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.
*
* We want to do the transformation in place.
* After the transformation, the left pointer of the tree node should point to its predecessor,
* and the right pointer should point to its successor.
*
* You should return the pointer to the smallest element of the linked list.
*
*
* Example 1:
* Input: root = [4,2,5,1,3]
* Output: [1,2,3,4,5]
*
* Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
*
*
* Example 2:
* Input: root = [2,1,3]
* Output: [1,2,3]
*
*
* Example 3:
* Input: root = []
* Output: []
* Explanation: Input is an empty tree. Output is also an empty Linked List.
*
*
* Example 4:
* Input: root = [1]
* Output: [1]
*
*
* Constraints:
*
* -1000 <= Node.val <= 1000
* Node.left.val < Node.val < Node.right.val
* All values of Node.val are unique.
* 0 <= Number of Nodes <= 2000
*/
public class BSTtoCircularDLL {
static class Node {
public int val;
public Node left;
public Node right;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val,Node _left,Node _right) {
val = _val;
left = _left;
right = _right;
}
};
public static Node treeToDoublyList(Node root) {
if (root == null)
return root;
Node dummy = new Node(-10001);
Node prev = dummy;
Node current = root;
Deque<Node> stack = new ArrayDeque<>();
// Do inorder traversal
while(current != null || !stack.isEmpty()) {
while(current != null) {
stack.push(current);
current = current.left;
}
Node temp = stack.pop(); // get the leftmost node
current = temp.right; // make current to traverse it right node
// Create connections for the poped node.
temp.left = prev;
prev.right = temp;
prev = temp;
}
// Circular connection for the head and tail of the circular DLL
prev.right = dummy.right;
dummy.right.left = prev;
return dummy.right;
}
public static void main(String[] args) {
Node root = new Node(4);
root.left = new Node(2);
root.right = new Node(5);
root.left.left = new Node(1);
root.left.right = new Node(3);
Node result = treeToDoublyList(root);
Node current = result;
while(current.right != result) {
System.out.print(current.val + " ");
current = current.right;
}
System.out.println(current.val);
}
}