Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

538. Convert BST to Greater Tree

Medium70.0% Acceptance
TreeDepth-First SearchBinary Search Tree
Asked by:
Facebook
ProblemSolutions (7)VideosCompanies (3)Notes

Problem Statement

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.

As a reminder, a binary search tree is a tree that satisfies these constraints:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Example 2:

Input: root = [0,null,1]
Output: [1,null,1]

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • -104 <= Node.val <= 104
  • All the values in the tree are unique.
  • root is guaranteed to be a valid binary search tree.

Note: This question is the same as 1038: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
F
A
Amazon
B
Bloomberg

Approach

In #538 Convert BST to Greater Tree, each node in a Binary Search Tree must be updated so that its value becomes the original value plus the sum of all greater values in the tree. The key observation comes from the BST property: an in-order traversal visits nodes in ascending order. If we reverse this traversal (right → node → left), we process nodes from the largest to the smallest.

Using a Depth-First Search (DFS) with reverse in-order traversal allows us to maintain a running cumulative sum of visited nodes. As each node is visited, we update its value using the accumulated sum before moving to smaller nodes. This approach ensures that when a node is processed, all greater values have already been accounted for.

The traversal visits every node exactly once, giving an efficient linear solution. Because recursion or an explicit stack is used for DFS, the extra memory depends on the height of the tree.

Complexity

ApproachTime ComplexitySpace Complexity
Reverse In-order DFS TraversalO(n)O(h)

Video Solution Available

NeetCode

View all video solutions

Solutions (7)

Reverse Inorder Traversal Approach

This approach leverages the property of Binary Search Trees where the nodes in the right subtree are greater than those in the left subtree. By performing a reverse inorder traversal (right -> root -> left), we can accumulate the sum of all nodes greater than the current node and update each node with this accumulated sum. The traversal ensures that we always process the greater nodes first.

Time Complexity: O(n), where n is the number of nodes in the BST, since we visit each node exactly once.
Space Complexity: O(h), where h is the height of the tree, due to the recursion stack used during traversal.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3struct TreeNode {
4    int val;
5    struct TreeNode *left;
6    struct TreeNode

Explanation

The C solution defines a helper function traverse that modifies the tree in place. This function is called with the root of the tree and maintains a running total of the sum of node values. We first recurse into the right subtree (greater values), add the current node's value to the sum, update the node's value to the sum, and finally recurse into the left subtree.

Morris Traversal Approach

The Morris Traversal technique allows in-order traversal of a binary tree without using extra space for recursion or a stack. It modifies the tree structure during the traversal, and at the end of the traversal, the original tree structure is restored.

For this problem, we adapt the Morris Traversal to traverse the tree in reverse inorder fashion and keep track of the sum of nodes greater than the current node.

Time Complexity: O(n), where n is the number of nodes. Although every edge is visited at most twice, the overall complexity remains linear.
Space Complexity: O(1) since no extra space is used apart from variables.

1class TreeNode:
2    def __init__(self, val=0,

Video Solutions

Watch expert explanations and walkthroughs

Convert BST to Greater Tree - Leetcode 538 - Python

NeetCode
9:3325,414 views

Asked By Companies

3 companies
F
Facebook
A
Amazon
B
Bloomberg

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Binary Tree Inorder TraversalEasy
Validate Binary Search TreeMedium
Recover Binary Search TreeMedium
Same TreeEasy
More similar problems

Related Topics

TreeDepth-First SearchBinary Search TreeBinary Tree

Problem Stats

Acceptance Rate70.0%
DifficultyMedium
Companies3

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Convert BST to Greater Tree asked in FAANG interviews?

Yes, variations of this problem appear in technical interviews at major tech companies. It tests understanding of BST properties, tree traversal strategies, and how to maintain state during DFS.

Why does reverse in-order traversal work for this problem?

In a BST, in-order traversal produces values in ascending order. Reversing the order processes nodes from largest to smallest, which means by the time you reach a node, you have already seen all greater values and can add their sum directly.

What is the optimal approach for Convert BST to Greater Tree?

The optimal approach is a reverse in-order traversal (right → node → left). Since a BST visited this way processes nodes from largest to smallest, you can maintain a running sum and update each node as you traverse. This ensures all greater values are already included when modifying a node.

What data structure is best for solving Convert BST to Greater Tree?

A Binary Search Tree combined with Depth-First Search is ideal for this problem. Using recursion or an explicit stack helps perform reverse in-order traversal efficiently while maintaining a cumulative sum of visited nodes.

*
right
;
7
}
;
8
9
void
traverse
(
struct
TreeNode
*
node
,
int
*
sum
)
{
10
if
(
node
==
NULL
)
return
;
11
traverse
(
node
->
right
,
sum
)
;
12
*
sum
+=
node
->
val
;
13
node
->
val
=
*
sum
;
14
traverse
(
node
->
left
,
sum
)
;
15
}
16
17
struct
TreeNode
*
convertBST
(
struct
TreeNode
*
root
)
{
18
int
sum
=
0
;
19
traverse
(
root
,
&
sum
)
;
20
return
root
;
21
}
left
=
None
,
right
=
None
)
:
3
self
.
val
=
val
4
self
.
left
=
left
5
self
.
right
=
right
6
7
class
Solution
:
8
def
convertBST
(
self
,
root
:
TreeNode
)
-
>
TreeNode
:
9
sum
=
0
10
node
=
root
11
while
node
is
not
None
:
12
if
node
.
right
is
None
:
13
sum
+=
node
.
val
14
node
.
val
=
sum
15
node
=
node
.
left
16
else
:
17
succ
=
node
.
right
18
while
succ
.
left
is
not
None
and
succ
.
left
!=
node
:
19
succ
=
succ
.
left
20
if
succ
.
left
is
None
:
21
succ
.
left
=
node
22
node
=
node
.
right
23
else
:
24
succ
.
left
=
None
25
sum
+=
node
.
val
26
node
.
val
=
sum
27
node
=
node
.
left
28
return
root

Explanation

This Python solution uses the Morris Traversal technique, which allows modifying the tree without using stack or recursion explicitly. The key idea is to establish a temporary link between each node and its inorder predecessor, traverse in reverse inorder, update the nodes, and then restore the tree structure by removing the temporary links.