【leetcode】1038. Binary Search Tree to Greater Sum Tree

题目如下:

Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val.

As a reminder, a binary search tree is a tree that satisfies these constraints:

  • The left subtree of a node contains only nodes with keys less than the node‘s key.
  • The right subtree of a node contains only nodes with keys greater than the node‘s key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

Input: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Note:

  1. The number of nodes in the tree is between 1 and 100.
  2. Each node will have value between 0 and 100.
  3. The given tree is a binary search tree.

解题思路:我的方法简单粗暴,第一次遍历树,把树中每个节点的值存入list;接下来再遍历一次,对于每个node,在list中找出所有值比自己大的元素的和,加上node自身的值,即为这个node的新值。

代码如下:

# 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):
    val_list = []
    def recursive(self,node):
        self.val_list.append(node.val)
        if node.right != None:
            self.recursive(node.right)
        if node.left != None:
            self.recursive(node.left)

    def reValue(self,node):
        inx = self.val_list.index(node.val)
        node.val += sum(self.val_list[inx+1:])
        if node.right != None:
            self.reValue(node.right)
        if node.left != None:
            self.reValue(node.left)

    def bstToGst(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if root != None:
            self.val_list = []
            self.recursive(root)
            self.val_list.sort()
            self.reValue(root)
        return root

原文地址:https://www.cnblogs.com/seyjs/p/11026162.html

时间: 2024-07-30 17:25:31

【leetcode】1038. Binary Search Tree to Greater Sum Tree的相关文章

【LeetCode】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

【LeetCode】Validate Binary Search Tree 解题报告

今天CSDN博客发生异常,折腾了大半天终于发出了这篇博文. [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a

【LeetCode】Recover Binary Search Tree 解题报告

[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? [解析] 题意:二叉搜索树中,有两个结点的位置

【LeetCode】Validate Binary Search Tree 二叉查找树的判断

题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树的所有点都小于或等于这个点的值,右子树的所有节点的值大于该节点的值: 2.最左节点值最小,最右节点值最大: 3.中序遍历结果值是一个非降的数列 问题:如果用Integer.MAX_VALUE会过不了测试用例,可是按照TreeNode中的定义,val值也是int呀,没办法用了Long,如果谁知道,麻烦

【leetcode】Recover Binary Search Tree

Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 中序

【leetcode】Validate Binary Search Tree(middle)

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【LeetCode】二叉查找树 binary search tree(共14题)

链接:https://leetcode.com/tag/binary-search-tree/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [220]Contains Duplicate III [315]Count of Smaller Numbers After Self [327]Count of Range Sum [352]Data Stream as Disjoint Intervals [493]

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's sh