码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 230. Kth Smallest Element in a BST


    Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

    Example 1:

    Input: root = [3,1,4,null,2], k = 1
    Output: 1
    

    Example 2:

    Input: root = [5,3,6,2,4,null,null,1], k = 3
    Output: 3
    

    Constraints:

    • The number of nodes in the tree is n.
    • 1 <= k <= n <= 104
    • 0 <= Node.val <= 104

    题目:给定一个二叉查找树,返回第K小的值

    思路:树的inorder遍历的变体。

    方法一:迭代法,代码:

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. int kthSmallest(TreeNode* root, int k) {
    15. int count = 0;
    16. stack stk;
    17. TreeNode* node = root;
    18. while(!stk.empty() || node){
    19. while(node){
    20. stk.push(node);
    21. node = node->left;
    22. }
    23. node = stk.top();
    24. stk.pop();
    25. count++;
    26. if(count == k) return node->val;
    27. node = node->right;
    28. }
    29. return -1;
    30. }
    31. };

    方法二,递归法,代码:

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. bool DFS(TreeNode* node, int k, int& pos, int& res){
    15. if(!node || pos > k) return false;
    16. bool left = DFS(node->left, k, pos, res);
    17. if(left) return true;
    18. pos++;
    19. if(pos == k){
    20. res = node->val;
    21. return true;
    22. }
    23. return DFS(node->right, k, pos, res);
    24. }
    25. int kthSmallest(TreeNode* root, int k) {
    26. int pos = 0, res = 0;
    27. DFS(root, k, pos, res);
    28. return res;
    29. }
    30. };

  • 相关阅读:
    Windows应急响应排查
    SpringBoot如何进行限流,老鸟们还可以这样玩
    不同设备的请求头信息UserAgent,Headers
    尝试搞懂 MySQL(一)
    【深度学习 | 计算机视觉】Focal Loss原理及其实践(含源代码)
    哪款取暖器省电效果好 家用哪个取暖好最实用
    【电压质量】提高隔离电源系统的电压质量(Simulink实现)
    linux课程第一课------命令的简单的介绍
    `dump` 和 `load` 以及 `dumps` 和 `loads`的用法
    Mysql数据库 12.SQL语言 触发器
  • 原文地址:https://blog.csdn.net/qing2019/article/details/126301362
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号