forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertBSTtoGreaterTre.java
74 lines (55 loc) · 2.06 KB
/
convertBSTtoGreaterTre.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
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original
BST is changed to the original key plus sum of all keys greater than the original key in BST.
Example:
Input: The root of a Binary Search Tree like this:
5
/ \
2 13
Output: The root of a Greater Tree like this:
18
/ \
20 13
//Recursive solution
Time complexity : O(n)
A binary tree has no cycles by definition, so convertBST gets called on each node no more than once.
Other than the recursive calls, convertBST does a constant amount of work, so a linear number of calls to convertBST
will run in linear time.
Space complexity : O(n)
Using the prior assertion that convertBST is called a linear number of times,
we can also show that the entire algorithm has linear space complexity. Consider the worst case,
a tree with only right (or only left) subtrees. The call stack will grow until the end of the longest
path is reached, which in this case includes all nn nodes.
public TreeNode convertBST(TreeNode root) {
dfs(root, 0);
return root;
}
public int dfs(TreeNode root, int val) {
if(root == null) return val;
int right = dfs(root.right, val);
int left = dfs(root.left, root.val + right);
root.val = root.val + right;
return left;
}
//iterative solution
class Solution {
public TreeNode convertBST(TreeNode root) {
int sum = 0;
TreeNode node = root;
Stack<TreeNode> stack = new Stack<TreeNode>();
while (!stack.isEmpty() || node != null) {
/* push all nodes up to (and including) this subtree's maximum on
* the stack. */
while (node != null) {
stack.add(node);
node = node.right;
}
node = stack.pop();
sum += node.val;
node.val = sum;
/* all nodes with values between the current and its parent lie in
* the left subtree. */
node = node.left;
}
return root;
}
}