【LeetCode】538. Convert BST to Greater Tree 解題報告(Python)
目錄
題目描述
Given 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 sum of all keys greater than the original key in BST.
Example:
Input: The root of a Binary Search Tree like this: 5 / \ 2 13 Output: The root of a Greater Tree like this: 18 / \ 20 13
題目大意
把BST的每個節點的值重新設定為所有比它值大的節點的值的和。
解題方法
遞迴
這個題要把每個節點變為比該節點值大(包含)的所有節點之和。
這個題的動機是我們應該先修改數值比較大的節點,然後修改數值比較小的節點。這樣做,才能保證,我們只需要一次遍歷,就把每個節點的值修改成了比它值更大的所有節點的和。
BST的右子樹都比該節點大,所以修改次序是右–>中–>左。用一個變數儲存遍歷過程中所有有節點之和就得到了所有的比當前把該節點的值更大的和,然後修改為該變數的值即可。
# 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):
def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
self.sum = 0
def afterOrder(cur):
if not cur: return
afterOrder(cur.right)
self.sum += cur.val
cur.val = self.sum
afterOrder(cur.left)
afterOrder(root)
return root
日期
2018 年 1 月 22 日
2018 年 11 月 13 日 —— 時間有點快
相關推薦
【LeetCode】538. Convert BST to Greater Tree 解題報告(Python)
目錄題目描述題目大意解題方法遞迴日期 題目描述 Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST i
【LeetCode】944. Delete Columns to Make Sorted 解題報告(Python)
目錄題目描述題目大意解題方法日期 題目描述 We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set
【LeetCode】849. Maximize Distance to Closest Person 解題報告(Python)
目錄題目描述題目大意解題方法日期 題目描述 In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty. Th
【LeetCode】669. Trim a Binary Search Tree 解題報告(Python)
作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 遞迴 日期 題目
【LeetCode】149. Max Points on a Line 解題報告(Python)
作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 字典+最大公約數 日期
【LeetCode】713. Subarray Product Less Than K 解題報告(Python)
題目描述: Your are given an array of positive integers nums. Count and print the number of (contiguous) subarrays where the product
【LeetCode】794. Valid Tic-Tac-Toe State 解題報告(Python)
題目描述: A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board positi
【LeetCode】957. Prison Cells After N Days 解題報告(Python)
作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 週期是14 日期
LeetCode:538. Convert BST to Greater Tree(一棵樹上加入所有的節點)
Given 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 sum of all
【LeetCode】967. Numbers With Same Consecutive Differences 解題報告(Python & C++)
作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 DFS 日期 題
LeetCode 538. Convert BST to Greater Tree
Given 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 sum of all keys
JavaScript刷LeetCode -- 538. Convert BST to Greater Tree
一、題目 Given 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 plu
LeetCode 538. Convert BST to Greater Tree (將 BST 轉換為Greater樹)
原題 Given 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 su
Leetcode——538. Convert BST to Greater Tree
題目原址 題目描述 Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to
538. Convert BST to Greater Tree
oot des gre all tput val greate ret con Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the origi
538 Convert BST to Greater Tree 把二叉搜索樹轉換為累加樹
arch rtb efi fin tac problem https 一個 htm 給定一個二叉搜索樹(Binary Search Tree),把它轉換成為累加樹(Greater Tree),使得每個節點的值是原來的節點值加上所有大於它的節點值之和。例如:輸入: 二叉搜索樹
538. Convert BST to Greater Tree(python+cpp)
題目: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the origin
538. Convert BST to Greater Tree - Easy
Given 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 sum of all keys
【LeetCode】958. Check Completeness of a Binary Tree 解題報告(Python)
作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 BFS DFS 日期
LeetCode - Convert BST to Greater Tree
Given 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 sum of all ke