Skip to content

Commit ae3eadd

Browse files
committed
added convert bst to greater tree solution
1 parent 46b45fc commit ae3eadd

5 files changed

Lines changed: 93 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# [LC 515. Find Largest Value in Each Tree Row - Medium](https://leetcode.com/problems/convert-bst-to-greater-tree/description/)
2+
3+
Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).
4+
5+
### Example 1
6+
7+
![Find Largest Value in Each Tree Row Example 1](https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg)
8+
9+
```
10+
Input: root = [1,3,2,5,3,null,9]
11+
Output: [1,3,9]
12+
```
13+
14+
### Example 2
15+
16+
```
17+
Input: root = [1,2,3]
18+
Output: [1,3]
19+
```
20+
21+
22+
### Constraints:
23+
24+
- The number of nodes in the tree will be in the range [0, 104].
25+
- -2^31 <= Node.val <= 2^31 - 1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import deque
2+
3+
def largestValues(root):
4+
if not root:
5+
return []
6+
7+
queue, result = deque([root]), []
8+
while queue:
9+
level_size, max_node_val = len(queue), float('-inf')
10+
for _ in range(level_size):
11+
curr_node = queue.popleft()
12+
max_node_val = max(max_node_val, curr_node.val)
13+
14+
if curr_node.left:
15+
queue.append(curr_node.left)
16+
if curr_node.right:
17+
queue.append(curr_node.right)
18+
result.append(max_node_val)
19+
return result

leetcode/src/binary_trees/lc_530_minimum_absolute_diff_in_bst/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Output: 1
1313

1414
### Example 2
1515

16-
![Minimum Absolute Difference in BST Example 1](https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg)
16+
![Minimum Absolute Difference in BST Example 2](https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg)
1717

1818
```
1919
Input: root = [1,0,48,null,null,12,49]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [LC 538. Convert BST to Greater Tree - Medium](https://leetcode.com/problems/convert-bst-to-greater-tree/description/)
2+
3+
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.
4+
5+
As a reminder, a binary search tree is a tree that satisfies these constraints:
6+
7+
- The left subtree of a node contains only nodes with keys less than the node's key.
8+
- The right subtree of a node contains only nodes with keys greater than the node's key.
9+
- Both the left and right subtrees must also be binary search trees.
10+
11+
### Example 1
12+
13+
![Convert BST to Greater Tree Example 1](https://assets.leetcode.com/uploads/2019/05/02/tree.png)
14+
15+
```
16+
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
17+
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
18+
```
19+
20+
### Example 2
21+
22+
```
23+
Input: root = [0,null,1]
24+
Output: [1,null,1]
25+
```
26+
27+
28+
### Constraints:
29+
30+
- The number of nodes in the tree is in the range [0, 10^4].
31+
- -10^4 <= Node.val <= 10^4
32+
- All the values in the tree are unique.
33+
- root is guaranteed to be a valid binary search tree.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def convertBST(root):
2+
def convertBstHelper(node):
3+
nonlocal running_sum
4+
if node is None: return
5+
6+
convertBstHelper(node.right)
7+
8+
node.val += running_sum
9+
running_sum = node.val
10+
11+
convertBstHelper(node.left)
12+
13+
running_sum = 0
14+
convertBstHelper(root)
15+
return root

0 commit comments

Comments
 (0)