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:

  1. Input: The root of a Binary Search Tree like this:
  2. 5
  3. / \
  4. 2 13
  5.  
  6. Output: The root of a Greater Tree like this:
  7. 18
  8. / \
  9. 20 13
  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution(object):
  9. '''
  10. def __init__(self):
  11. self.total=0
  12.  
  13. def convertBST(self, root):
  14. """
  15. :type root: TreeNode
  16. :rtype: TreeNode
  17. """
  18. if not root:
  19. return root
  20. self.convertBST(root.right)
  21. self.total+=root.val
  22. root.val=self.total
  23. self.convertBST(root.left)
  24. return root
  25. '''
  26. def convertBST(self, root):
  27. """
  28. :type root: TreeNode
  29. :rtype: TreeNode
  30. """
  31.  
  32. total=0
  33. stack=[]
  34. node=root
  35. while node is not None or stack:
  36. while node is not None:
  37. stack.append(node)
  38. node=node.right
  39. node=stack.pop()
  40. total+=node.val
  41. node.val=total
  42. node=node.left
  43. return root

  

[LeetCode&Python] Problem 860. Convert BST to Greater Tree的更多相关文章

  1. 【leetcode_easy】538. Convert BST to Greater Tree

    problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完

  2. LN : leetcode 538 Convert BST to Greater Tree

    lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...

  3. 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. 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 ...

  5. [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  6. 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 origi ...

  7. [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 ...

  8. 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 B ...

  9. 【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 orig ...

随机推荐

  1. Qt 之 去除窗口部件被选中后的焦点虚线框

    转自: https://blog.csdn.net/goforwardtostep/article/details/53420529 https://blog.csdn.net/caoshangpa/ ...

  2. 使用Redis数据库(1)(三十三)

    Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, So ...

  3. spring boot cloud

    eclipse spring boot 项目创建 https://www.cnblogs.com/shuaihan/p/8027082.html https://www.cnblogs.com/LUA ...

  4. MINIUI应用

    MINIUI是一款优秀的JS前端web框架,提供丰富.强大控件库,能快速开发企业级Web应用软件. 属于付费插件. 如果有兴趣推荐去这个网址看看.MiniUI 在线示例  http://www.min ...

  5. 【Query】使用java对mysql数据库进行查询操作

    操作步骤: 1.加载数据库驱动(先在工程里加载数据库对应的驱动包) 2.获取连接 3.根据连接建立一个可执行sql的对象 4.执行sql语句 5.关闭连接 代码: package database; ...

  6. 前端基础之jQuery事件

    一.常用事件 click(function(){...}) hover(function(){...}) blur(function(){...}) focus(function(){...}) ch ...

  7. Java 集成开发环境的介绍及下载

    集成开发环境(integrated development environment,JDE) 之前成功运行了Java小程序是经历了先在笔记本中编写源代码,然后通过命令行运行打开javac编译源文件, ...

  8. x多进程

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' from multiprocessing import Process import os #子进 ...

  9. MeshLab 编译

    1.需要以下:  MeshLab 1.3.3  下载地址 http://sourceforge.net/projects/meshlab/files/meshlab Win7 X64  Visual ...

  10. Spring boot 导出Excel

    Html页面: window.location.href="adjectfkController/exportTemplate?adjOrg="+ adjOrg +"&a ...