-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncreasingOrder.java
More file actions
48 lines (37 loc) · 859 Bytes
/
IncreasingOrder.java
File metadata and controls
48 lines (37 loc) · 859 Bytes
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
package DFS;
import java.util.Stack;
/**
* Author - archit.s
* Date - 03/09/18
* Time - 11:27 AM
*/
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class IncreasingOrder {
public TreeNode increasingBST(TreeNode root) {
Stack<TreeNode> s = new Stack<>();
TreeNode prev= null, head= null;
TreeNode cur = root;
while(cur != null || !s.empty()){
while(cur!=null){
s.push(cur);
cur = cur.left;
}
cur = s.pop();
if(head == null){
head = cur;
}
cur.left = null;
if(prev != null){
prev.right = cur;
}
prev = cur;
cur = cur.right;
}
return head;
}
}