LeetCode--538. Convert BST to Greater Tree(将二叉搜索树转为更大的二叉搜索树)

1人阅读 评论(0) 收藏 举报
分类:

题目:
给定一棵二叉搜索树,返回一个更大的二叉搜索树。使得每一个节点的值都为之前该节点的值加上之前树中比该节点大的所有数之和。样例如下:


解题思路:
已知二叉搜索树的特性:对于每个节点来说,该节点的值必定大于该节点左子树的所有数,也必定小于该节点右子树的所有数。所以对该二叉搜索树进行后序遍历,并保存当前遍历过的所有节点之和,将其加到遍历到的节点上即可。
代码(Python):
# 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
        """
        def ReadTree(root,temp_sum):
            if root==None:
                return temp_sum
            if root!=None:
                temp_sum = ReadTree(root.right,temp_sum)
                root.val = root.val+temp_sum
                temp_sum = root.val
                temp_sum = ReadTree(root.left,temp_sum)
                return temp_sum
        
        ReadTree(root,0)
        return root
            


查看评论

LintCode SortedArrayToBST 把排序数组转换为高度最小的二叉搜索树

LintCode SortedArraytoBST 将有序数组转化为高度最小的二叉搜索树
  • wutingyehe
  • wutingyehe
  • 2015-06-18 16:26:05
  • 1474

【LeetCode】538. Convert BST to Greater Tree

问题描述问题链接:https://leetcode.com/problems/convert-bst-to-greater-tree/#/descriptionGiven a Binary Searc...
  • mrbcy
  • mrbcy
  • 2017-03-21 13:02:11
  • 2503

LeetCode538. Convert BST to Greater Tree

Convert BST to Greater Tree 1、原题 Given a Binary Search Tree (BST), convert it to a Greater Tree su...
  • u010217031
  • u010217031
  • 2017-03-26 14:59:43
  • 380

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 B...
  • qazwyc
  • qazwyc
  • 2017-03-19 16:08:21
  • 2266

[leetcode]- 538. Convert BST to Greater Tree

刷这道题的时候,并没有想到中序遍历的方法,对于树的遍历算法应该去复习一下了没有思路,于是上网查找了一些解法,下面的解法简单机智。 转载地址:http://blog.csdn.net/qazwyc...
  • ljh0302
  • ljh0302
  • 2017-03-29 20:29:08
  • 320

LeetCode Weekly Contest 24 之 538.Convert BST to Greater Tree

LeetCode Weekly Contest 24赛题本次周赛主要分为一下4道题: 543 Diameter of Binary Tree (4分) 538 Convert BST to Great...
  • u014688145
  • u014688145
  • 2017-03-19 19:33:36
  • 750

Convert BST to Greater Tree

题目: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the o...
  • supreLee
  • supreLee
  • 2017-04-04 18:42:21
  • 395

LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BS...
  • u012249528
  • u012249528
  • 2015-07-30 09:19:54
  • 939

[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 B...
  • TstsUgeg
  • TstsUgeg
  • 2017-03-21 16:52:21
  • 284

二叉搜索树(BST)的创建、插入、查找和删除

树的结构体定义 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), le...
  • zmq570235977
  • zmq570235977
  • 2016-06-21 15:48:49
  • 2943
    个人资料
    持之以恒
    等级:
    访问量: 9468
    积分: 1104
    排名: 4万+
    文章分类
    文章存档