当前位置:网站首页>Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
2022-06-29 23:51:00 【Steven Devin】
Binary search tree 230. Binary search tree K Small elements 1038. From binary search tree to larger sum tree
230. Binary search tree K Small elements
Their thinking :
- Middle order traversal of binary search tree , Is the ascending order of nodes , This is also the characteristic of binary search tree .
- Set a value , To record whether the number of layers traversed is K.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
self.count = 0
self.res = 0
self.traverse(root,k)
return self.res
def traverse(self, root, k):
if not root:
return
self.traverse(root.left,k)
self.count += 1
if k == self.count:
self.res = root.val
return
self.traverse(root.right,k)
1038. From binary search tree to larger sum tree

Their thinking :
- Or use the binary search tree in order to traverse the characteristics , But the middle order traversal first traverses the left subtree , We can't calculate the cumulative sum greater than or equal to the current node .
- So reverse the traversal order of the left and right subtrees .
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def bstToGst(self, root: TreeNode) -> TreeNode:
self.res = 0
self.traverse(root)
return root
def traverse(self, root):
if not root:
return None
self.traverse(root.right) # First traverse the right subtree
self.res += root.val
root.val = self.res
self.traverse(root.left)
边栏推荐
- 关于 Apache 的 25 个初中级面试题
- 手机开户一般哪个证券公司好?另外,手机开户安全么?
- Database - playful data -pgsql uses UUID as primary key
- 为什么 JSX 语法这么香?
- 机器学习:VC维的概念和用途
- Sword finger offer 15 Number of 1 in binary
- shell-位置参数变量和预定义变量
- Leetcode(680)——验证回文字符串 Ⅱ
- Implementation principle of dynamic agent
- High performance and high availability computing architecture of "microblog comments" in microblog system
猜你喜欢

Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column

@Scheduled annotation pit, I stepped on it for you

High performance and high availability computing architecture of "microblog comments" in microblog system

漫画安全HIDS、EDR、NDR、XDR

Matlab exercises -- program control process exercise

雲和恩墨蓋國强,識別它、抓住它,在國產數據庫沸騰以前

【微信小程序】认识小程序项目的基本组成结构

This simple little function saves 213 hours for our production research team in half a year

Remember the process of checking online MySQL deadlock. You should not only know curd, but also know the principle of locking

High performance and high availability computing architecture of "Weibo comments"
随机推荐
Solr基础操作
Under the epidemic, I left my job for a year, and my income increased 10 times
shell-运算符
Applet plug-in access, development and precautions
MetaQ集群安裝測試
LC:最大子数组和
手机开户一般哪个证券公司好?另外,手机开户安全么?
Solr basic operation
Paper writing tool: latex online website
RRDTOOL draws MRTG log data
Gradle连载7-配置签名
Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages
采购数智化爆发在即,支出宝“3+2“体系助力企业打造核心竞争优势
[wechat applet] understand the basic composition of the applet project
Speech signal processing (II): phonation physiology, auditory physiology and auditory psychology
除子
[LeetCode] 只出现一次的数字【136】
提供有效的绩效评估
Leetcode(76)——最小覆盖子串
Sword finger offer 14- ii Cutting rope II