-
Notifications
You must be signed in to change notification settings - Fork 0
/
1038-Binary-Search-Tree-to-Greater-Sum-Tree.py
56 lines (49 loc) · 1.39 KB
/
1038-Binary-Search-Tree-to-Greater-Sum-Tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
ListNode = []
def bstToGst(self, root):
self.ListNode = []
self.dfs( root )
SumNum = 0
for node in self.ListNode:
SumNum += node.val
node.val = SumNum
return root
def dfs(self, node):
if node:
if node.right:
self.dfs( node.right )
self.ListNode.append( node )
if node.left:
self.dfs( node.left )
class SolutionII(object):
def bstToGst(self, root):
self.bstToGst2( root, 0 )
return root
def bstToGst2( self, root, SumNum ):
if root.right:
SumNum = self.bstToGst2( root.right, SumNum )
root.val += SumNum
SumNum = root.val
if root.left:
SumNum = self.bstToGst2( root.left, SumNum )
return SumNum
class SolutionIII(object):
val = 0
def bstToGst(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root.right:
self.bstToGst( root.right )
root.val += self.val
self.val = root.val
if root.left:
self.bstToGst(root.left)
return root