博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 1038. Binary Search Tree to Greater Sum Tree 解法 python
阅读量:2492 次
发布时间:2019-05-11

本文共 3439 字,大约阅读时间需要 11 分钟。

一.问题描述

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.

二.解题思路

我的解题思路是:

首先计算出大于或等于原根节点的值,然后我们可以发现,对此根节点来说,它右边的部分的节点计算只需要减去它的父节点的节点值,然后如果有左孩子,再减去左孩子的和就是大于或等于它的值。

对于原根节点的左部分来说(注意是原根节点),每个节点的更新是父节点的值加上自己根节点的值,如果有右孩子,还要加上右孩子部分的和。

具体的更新看代码可能更明白一些。

更新:一个更明了的方法

对每个节点来说,大于或等于他的值的和都是,它如果是左孩子,就是本身的值加上右子树的值加上父节点的值加上父节点右子树的值,如果节点是右孩子,就是它本身的值加上右节点的值。可以看到我们中序遍历到最右子节点,先计算节点的右分支,然后计算当前节点,然后再更新左孩子

更多leetcode算法题解法请关注我的专栏或关注我

欢迎大家一起套路一起刷题一起ac

三.源码

# Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution:    def bstToGst(self, root: TreeNode) -> TreeNode:        new_root=TreeNode(None)        sum_num=root.val        sum_num+=self.findEqualAndLarge(root.right)        new_root.val=sum_num        if root.left:            tmp1=TreeNode(None)            new_root.left=tmp1            self.updateLeftTree(root.left,sum_num,tmp1)        if root.right:            tmp2=TreeNode(None)            new_root.right=tmp2            self.updateRightTree(root.right,sum_num-root.val,tmp2)        return new_root            def updateLeftTree(self,root,sum_num,new_root):        sum_num+=root.val        new_root.val=sum_num        if root.left==None and root.right==None:            return        if root.right:            new_root.val+=self.findEqualAndLarge(root.right)            temp1=TreeNode(None)            new_root.right=temp1            self.updateLeftTree(root.right,sum_num-root.val,temp1)        if root.left:            temp2=TreeNode(None)            new_root.left=temp2            self.updateLeftTree(root.left,new_root.val,temp2)        def updateRightTree(self,root,sum_num,new_root):        new_root.val=sum_num        if root.left==None and root.right==None:            return        if root.left:            new_root.val-=self.findEqualAndLarge(root.left)            temp1=TreeNode(None)            new_root.left=temp1            self.updateRightTree(root.left,sum_num,temp1)        if root.right:            temp2=TreeNode(None)            new_root.right=temp2            self.updateRightTree(root.right,new_root.val-root.val,temp2)        def findEqualAndLarge(self,root):        sum_num=root.val        if root.left==None and root.right==None:            return sum_num        if root.left:            sum_num+=self.findEqualAndLarge(root.left)        if root.right:            sum_num+=self.findEqualAndLarge(root.right)        return sum_num
#version 2n = 0def bstToGst(self, root):    """    :type root: TreeNode    :rtype: TreeNode    """    if root:        self.bstToGst(root.right)        root.val = self.n = self.n + root.val        self.bstToGst(root.left)    return root

 

转载地址:http://mqmrb.baihongyu.com/

你可能感兴趣的文章
markdown学习/mou
查看>>
CentOS 搭建 LAMP服务器
查看>>
很多人都不知道,其实博客园给我们博客开了二级域名
查看>>
tiny4412 linux+qtopia nfs网络文件系统的挂载
查看>>
Web UI 自动化测试环境搭建 (转载自51测试天地第三十九期上)
查看>>
在Bootstrap开发框架中使用bootstrap-datepicker插件
查看>>
String类中IndexOf与SubString
查看>>
记录下Linux难记实用的命令
查看>>
react 路由 react-router-dom
查看>>
Java工具类——通过配置XML验证Map
查看>>
Leetcode::Subsets
查看>>
JAVA 重写&重载/多态/抽象类/封装/接口/包
查看>>
关于js的function.来自百度知道的回答,学习了.
查看>>
学习正则表达式
查看>>
linux高级IO
查看>>
angualarjsdemo
查看>>
【C#】解析C#中JSON.NET的使用
查看>>
PyQt中从RAM新建QIcon对象 / Create a QIcon from binary data
查看>>
HTML5拖放API
查看>>
【Django】Django web项目部署(Nginx+uwsgi)
查看>>