[LeetCode][Python3] 538. Convert BST to Greater Tree
2018. 10. 7. 23:29 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/convert-bst-to-greater-tree/description/
My Solution :
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def __init__(self):
self.total = 0
def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root:
self.convertBST(root.right)
self.total += root.val
root.val = self.total
self.convertBST(root.left)
return root
Comment :
BST이기 때문에 right, root, left 순서로 순회하면 내림차순으로 방문하게 된다. 따라서 누적 합을 계속해서 root.val로 업데이트 해주면 된다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 543. Diameter of Binary Tree (0) | 2018.10.09 |
---|---|
[LeetCode][Python3] 617. Merge Two Binary Trees (0) | 2018.10.08 |
[LeetCode][Python3] 581. Shortest Unsorted Continuous Subarray (0) | 2018.10.08 |
[LeetCode][Python3] 572. Subtree of Another Tree (0) | 2018.10.07 |
[LeetCode][Python3] 438. Find All Anagrams in a String (0) | 2018.10.07 |
[LeetCode][Python3] 437. Path Sum III (0) | 2018.10.07 |
[LeetCode][Python3] 283. Move Zeroes (0) | 2018.10.05 |
[LeetCode][Python3] 234. Palindrome Linked List (0) | 2018.10.04 |
최근에 달린 댓글 최근에 달린 댓글