Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 686 Bytes

1038.Binary-Search-Tree-to-Greater-Sum-Tree.md

File metadata and controls

32 lines (25 loc) · 686 Bytes

1038. Binary Search Tree to Greater Sum Tree

Difficulty: Medium

URL

https://leetcode.com/problems/uncrossed-lines/

Solution

Approach 1: Inorder Traversal

The code is shown below:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    cur_sum = 0
    def bstToGst(self, root: TreeNode) -> TreeNode:
        if not root:
            return root
        self.bstToGst(root.right)
        self.cur_sum += root.val
        root.val = self.cur_sum
        self.bstToGst(root.left)
        return root