当前位置:网站首页>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)
边栏推荐
- Label Troubleshooting: unable to open the marked image
- Solr基础操作4
- Matplotlib plt Hist() parameter explanation
- 333333333333333333333333333333
- Software testing interface testing JMeter 5.5 installation tutorial
- 6.29日刷题题解
- MetaQ集群安装测试
- Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here
- Gradle serialization 7- configuration signature
- Leetcode(680)——验证回文字符串 Ⅱ
猜你喜欢

High performance and high availability computing architecture of "Weibo comments"

“微博评论”的高性能高可用计算架构

Matplotlib plt Hist() parameter explanation

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

333333333333333333333333333333

云服务器的安全设置常识

Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally

@Scheduled注解的坑,我替你踩了

Incluxdb time series database system

Why is JSX syntax so popular?
随机推荐
Speech signal processing (II): phonation physiology, auditory physiology and auditory psychology
FPGA开发(1)——串口通信
二叉树的序列化 力扣 297. 二叉树的序列化与反序列化 652. 寻找重复的子树
Is it safe to open a stock account? Shanghai stock account opening.
Collection! Have you ever used these tools to improve programmer productivity?
6.29 problem solving
Test d'installation du cluster metaq
[LeetCode] 只出现一次的数字【136】
Matplotlib plt Hist() parameter explanation
漫画安全HIDS、EDR、NDR、XDR
Pytest initializing and cleaning up the environment
Set up security groups, record domain names, and apply for SSL certificates
简单理解B树和B+树
High performance and high availability computing architecture of "Weibo comments"
Remember the process of checking online MySQL deadlock. You should not only know curd, but also know the principle of locking
小程序插件接入、开发与注意事项
What is online account opening? In addition, is it safe to open a mobile account?
Gradle连载7-配置签名
Solr基础操作5
Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages