Skip to content

Commit ceff082

Browse files
committed
Added Q538
1 parent 03fac0c commit ceff082

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
id: convert-bst-to-greater-tree
3+
title: Convert BST to Greater Tree
4+
sidebar_label: 0538-Convert-BST-to-Greater-Tree
5+
tags:
6+
- Tree
7+
- Depth-First Search
8+
- Binary Search Tree
9+
- Binary Tree
10+
description: "Given the root of 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 the sum of all keys greater than the original key in BST."
11+
---
12+
13+
## Problem
14+
15+
Given the root of 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 the sum of all keys greater than the original key in BST.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
**Input:** `root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]`
22+
**Output:** `[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]`
23+
24+
**Example 2:**
25+
26+
**Input:** `root = [0,null,1]`
27+
**Output:** `[1,null,1]`
28+
29+
### Constraints
30+
31+
- The number of nodes in the tree is in the range `[0, 10^4]`.
32+
- `-10^4 <= Node.val <= 10^4`
33+
- All the values in the tree are unique.
34+
- `root` is guaranteed to be a valid binary search tree.
35+
36+
---
37+
38+
## Approach
39+
40+
To convert a BST to a Greater Tree, we need to accumulate the sum of all nodes that are greater than the current node. This can be efficiently done using a reverse in-order traversal (right-root-left), which processes nodes from the largest to the smallest.
41+
42+
### Steps:
43+
44+
1. Initialize a variable to keep track of the running sum.
45+
2. Perform a reverse in-order traversal of the tree.
46+
3. During the traversal, update the value of each node to include the running sum.
47+
4. Update the running sum with the value of the current node.
48+
49+
### Solution
50+
51+
#### Java Solution
52+
53+
```java
54+
public class TreeNode {
55+
int val;
56+
TreeNode left;
57+
TreeNode right;
58+
TreeNode(int x) { val = x; }
59+
}
60+
61+
class Solution {
62+
private int sum = 0;
63+
64+
public TreeNode convertBST(TreeNode root) {
65+
if (root != null) {
66+
convertBST(root.right);
67+
sum += root.val;
68+
root.val = sum;
69+
convertBST(root.left);
70+
}
71+
return root;
72+
}
73+
}
74+
```
75+
#### C++ Solution
76+
77+
```cpp
78+
#include <iostream>
79+
using namespace std;
80+
81+
struct TreeNode {
82+
int val;
83+
TreeNode *left;
84+
TreeNode *right;
85+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
86+
};
87+
88+
class Solution {
89+
private:
90+
int sum = 0;
91+
public:
92+
TreeNode* convertBST(TreeNode* root) {
93+
if (root != nullptr) {
94+
convertBST(root->right);
95+
sum += root->val;
96+
root->val = sum;
97+
convertBST(root->left);
98+
}
99+
return root;
100+
}
101+
};
102+
```
103+
#### Python Solution
104+
105+
```python
106+
# Definition for a binary tree node.
107+
class TreeNode:
108+
def __init__(self, val=0, left=None, right=None):
109+
self.val = val
110+
self.left = left
111+
self.right = right
112+
113+
class Solution:
114+
def convertBST(self, root: TreeNode) -> TreeNode:
115+
self.sum = 0
116+
117+
def traverse(node):
118+
if node:
119+
traverse(node.right)
120+
self.sum += node.val
121+
node.val = self.sum
122+
traverse(node.left)
123+
124+
traverse(root)
125+
return root
126+
```
127+
128+
### Complexity Analysis
129+
**Time Complexity:** O(n)
130+
>Reason: Each node is visited once during the traversal.
131+
132+
**Space Complexity:** O(h)
133+
>Reason: The space complexity is determined by the recursion stack, which in the worst case (unbalanced tree) is O(n), but on average (balanced tree) is O(log n).
134+
135+
### References
136+
**LeetCode Problem:** Convert BST to Greater Tree

0 commit comments

Comments
 (0)