Skip to content

Commit 82e7cb9

Browse files
committed
[LeetCode Sync] Runtime - 4 ms (39.70%), Memory - 19.8 MB (63.87%)
1 parent 9614278 commit 82e7cb9

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>Given the <code>root</code> 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.</p>
2+
3+
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints:</p>
4+
5+
<ul>
6+
<li>The left subtree of a node contains only nodes with keys <strong>less than</strong> the node&#39;s key.</li>
7+
<li>The right subtree of a node contains only nodes with keys <strong>greater than</strong> the node&#39;s key.</li>
8+
<li>Both the left and right subtrees must also be binary search trees.</li>
9+
</ul>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/02/tree.png" style="width: 500px; height: 341px;" />
14+
<pre>
15+
<strong>Input:</strong> root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
16+
<strong>Output:</strong> [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
17+
</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> root = [0,null,1]
23+
<strong>Output:</strong> [1,null,1]
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
31+
<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
32+
<li>All the values in the tree are <strong>unique</strong>.</li>
33+
<li><code>root</code> is guaranteed to be a valid binary search tree.</li>
34+
</ul>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Note:</strong> This question is the same as 1038: <a href="https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/" target="_blank">https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/</a></p>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
9+
# create a variable to track the maximum sum that will be assigned to the last node
10+
sum = 0
11+
12+
# we will start with the root
13+
def helper(root):
14+
nonlocal sum
15+
16+
# if root is not null, run the loop at the greater side (right)
17+
# now sum will be updated with right's sum
18+
# append the value to the root.
19+
20+
# after that get the current sum and traverse to the left
21+
if root:
22+
helper(root.right)
23+
root.val += sum
24+
25+
sum = root.val
26+
helper(root.left)
27+
28+
return root
29+
30+
return helper(root)

0 commit comments

Comments
 (0)