当前位置:网站首页>Leetcode problem solving - 230 The k-th smallest element in the binary search tree
Leetcode problem solving - 230 The k-th smallest element in the binary search tree
2022-07-03 21:39:00 【Snowy solitary boat】
public int kthSmallest(TreeNode root, int k) {
int i = 1;
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
while (!stack.isEmpty()||node!=null){
while (node!=null){
stack.push(node);
node = node.left;
}
node = stack.pop();
if (i==k) return node.val;
i++;
node = node.right;
}
return -1;
}
Ideas : It is improved on the basis of the medium order iteration scheme :
- Judge at the access element , And count
- When i==k, That is, the current number of trees K Element time , Returns the value of the current node
边栏推荐
- 2022-02-15 Daily: 2022 AAAI fellow release
- Mysql database ----- common commands of database (based on database)
- [vulnhub shooting range] impulse: lupinone
- Qualcomm platform WiFi update disconnect end open event
- [secretly kill little buddy pytorch20 days -day02- example of image data modeling process]
- Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
- 常用sql集合
- 运维各常用命令总结
- Intimacy communication -- [repair relationship] - use communication to heal injuries
- 内存分析器 (MAT)
猜你喜欢
leetcode-540. A single element in an ordered array
Intimacy communication -- [repair relationship] - use communication to heal injuries
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks
Collections SQL communes
Nmap and masscan have their own advantages and disadvantages. The basic commands are often mixed to increase output
Hcie security Day10: six experiments to understand VRRP and reliability
MySQL——索引
运维各常用命令总结
Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files
treevalue——Master Nested Data Like Tensor
随机推荐
Dahua series books
Go learning notes (4) basic types and statements (3)
C程序设计的初步认识
Compréhension de la technologie gslb (Global Server load balance)
MySQL——idea连接MySQL
An expression that regularly matches one of two strings
MySQL - database backup
Redis data migration (II)
Collections SQL communes
How PHP drives mongodb
Preliminary understanding of C program design
Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files
Pengcheng cup Web_ WP
MySQL——JDBC
Mysql - - Index
leetcode-540. A single element in an ordered array
Global and Chinese market of wireless hard disk 2022-2028: Research Report on technology, participants, trends, market size and share
What is the maximum number of concurrent TCP connections for a server? 65535?
Collection | pytoch common loss function disassembly
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?