-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIncreasingOrderSearchTree.java
54 lines (44 loc) · 1.37 KB
/
IncreasingOrderSearchTree.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
package com.hvdbs.leetcode.solution.java;
import com.hvdbs.savra.statsgenerator.CodeInfo;
import com.hvdbs.savra.statsgenerator.enums.Complexity;
import com.hvdbs.savra.statsgenerator.enums.Difficulty;
@CodeInfo(
difficulty = Difficulty.EASY,
name = "Increasing Order Search Tree",
url = "https://leetcode.com/problems/increasing-order-search-tree",
spaceComplexity = Complexity.ConstantComplexity.LOGARITHMIC,
timeComplexity = Complexity.ConstantComplexity.LINEAR
)
public class IncreasingOrderSearchTree {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
TreeNode current;
public TreeNode increasingBST(TreeNode root) {
TreeNode newRoot = new TreeNode();
current = newRoot;
inOrderTraverse(root);
return newRoot.right;
}
private void inOrderTraverse(TreeNode root) {
if (root == null) {
return;
}
inOrderTraverse(root.left);
current.right = new TreeNode(root.val);
current = current.right;
inOrderTraverse(root.right);
}
}