Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

样例

Given root = {1,2}, k = 2, return 2.

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ class Solution {
public:
/**
* @param root: the given BST
* @param k: the given k
* @return: the kth smallest element in BST
*/ //迭代遍历到最后(思考如何提前终止)
int count = 0;
int res;
int kthSmallest(TreeNode * root, int k) {
// write your code here
if (root->left != NULL) kthSmallest(root->left, k);
if (++count == k) res = root->val; //①
if (root->right != NULL) kthSmallest(root->right, k);
return res;
} //循环
int kthSmallest(TreeNode * root, int k) {
// write your code here
std::stack<TreeNode*> sta;
while (true) {
while (root) {
sta.push(root);
root = root->left;
}
if (sta.empty()) break;
root = sta.top();
sta.pop();
if(--k==0) return root->val;
root = root->right;
}
}
};

902. Kth Smallest Element in a BST的更多相关文章

  1. 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

  2. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  3. LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

    230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...

  4. 【刷题-LeetCode】230. Kth Smallest Element in a BST

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

  5. [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  6. Leetcode Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  7. Leetcode 230. Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  8. Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  9. 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

随机推荐

  1. 【English】20190306

    Delivery team交付团队consumption消费[kənˈsʌmpʃən] The Consulting Delivery team is focused on delivering va ...

  2. HBase1.0.1.1 API与原来有所不同

    package com.felix.hbaseapi_test; /* 这是旧版的 API操作 */ public class hbaseapifelix { public static final ...

  3. 设计模式のIOC(控制反转)

    一.什么是Ioc IoC(Inverse of Control)的字面意思是控制反转,它包括两个内容: 控制.反转 可以假设这样一个场景:火车运货,不同类型的车厢运送不同类型的货物,板车运送圆木,罐车 ...

  4. linux快速搭建

    ------------------------------------------ 转载内容 --------------------- Linux升级命令有两个分别是yum upgrade和yum ...

  5. CSDN社区之星专訪:我的蜕变之路

    摘要:社区之星第49期採訪了爱奇艺研发总监陆其明.他分享了个人生涯.管理和程序猿素质等方面的看法.管理上他觉得对人要领导.要管的是事.而在程序猿素质上.他则引用Jeff Atwood的话称,成为优秀程 ...

  6. Druid、BoneCP、DBCP、C3P0等主流数据库对比

    关键功能 Druid BoneCP DBCP C3P0 Proxool JBoss LRU 是 否 是 否 是 是 PSCache 是 是 是 是 否 是 PSCache-Oracle-Optimiz ...

  7. (predicted == labels).sum().item()作用

    ⚠️(predicted == labels).sum().item()作用,举个小例子介绍: # -*- coding: utf-8 -*-import torch import numpy as ...

  8. fastJson 之 JSONPath使用

    1. JSONPath介绍 官网地址: https://github.com/alibaba/fastjson/wiki/JSONPath fastjson 1.2.0之后的版本支持JSONPath. ...

  9. CRC-16 (Modbus)

    typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; #defi ...

  10. Node.js这么下去...

    Node.js是基于javascript的.event驱动的单进程服务器(也能实现cluster模式,只要一个fork()语句,类似于C语言的进程创建). 所以大胆估计:Node.js会把很多大网站吞 ...