leecode第二百三十题(二叉搜索树中第K小的元素)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int k_time;
    int temp;
    void kth_min(TreeNode* root)
    {
        if(root->left!=NULL)//不停找左节点
            kth_min(root->left);

        if(k_time>0)//查看k减少到0没
            k_time--;
        else
            return;
        if(k_time==0)//如果为0,把当前节点赋给temp
            temp=root->val;

        if(root->right!=NULL)
            kth_min(root->right);
    }

    int kthSmallest(TreeNode* root, int k) {
        k_time=k;
        kth_min(root);
        return temp;

    }
};

分析:

中序进行一半,找到k值就返回。

原文地址:https://www.cnblogs.com/CJT-blog/p/10722640.html

时间: 2024-07-30 23:00:40

leecode第二百三十题(二叉搜索树中第K小的元素)的相关文章

leetcode 二叉搜索树中第K小的元素 python

二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / 1 4   2 输出: 1 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / 3 6 / 2 4 / 1 输出: 3 进阶:如果二叉搜索树经常被修改(插入/删除操作)并且

230. 二叉搜索树中第K小的元素

230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 解题思路 中序遍历,利用Python3中提供的生成器方法: 中序遍历,判断存储结点值的数组是否到到k,则表明访问的一个结点就是第k个最小的元素: 先获取跟结点处于的位置(第几个最小的元素),如果它比k小,则从右子结点中找,如果它比k大,则从左子节点中找: 实现 class Solution:    

230 Kth Smallest Element in a BST 二叉搜索树中第K小的元素

给定一个二叉搜索树,编写一个函数kthSmallest来查找其中第k个最小的元素. 注意:你可以假设k总是有效的,1≤ k ≤二叉搜索树元素个数. 进阶:如果经常修改二叉搜索树(插入/删除操作)并且你需要频繁地找到第k小值呢? 你将如何优化kthSmallest函数? 详见:https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 方法一:递归实现 /** * Definition for a binary

二叉搜索树中第K小的元素

给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / 1 4   2 输出: 1 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / 3 6 / 2 4 / 1 输出: 3 一个中序遍历的搜索,递归或者栈. /** * Definition for a

leetcode 230二叉搜索树中第k小的元素

通过stack进行中序遍历迭代,timeO(k),spaceO(1) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ /** BST中序遍历是从小到大排列的因此对其进行中序遍历然后取第k个元素: **/

leecode第二百三十八题(除自身以外数组的乘积)

class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { int len=nums.size(); vector<int> res; for(int i=0,temp=1;i<len;i++)//把前面的乘起来,暂存起来 { res.push_back(temp); temp=temp*nums[i]; } for(int i=len-1,temp=1;i>

二叉搜索树中的常用方法

1 package Tree; 2 3 import org.junit.Test; 4 5 class TreeNode { 6 7 int val = 0; 8 TreeNode left = null; 9 TreeNode right = null; 10 11 public TreeNode(int val) { 12 this.val = val; 13 14 } 15 16 } 17 18 public class BinarySearchTree { 19 20 /** 21 *

[LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null. 给一个二叉搜索树和它的一个节点,找出它的中序后继节点,如果没有返回null. 解法1: 用中序遍历二叉搜索树,当找到root.val = p.val的时

Leetcode 701. 二叉搜索树中的插入操作

题目链接 https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树. 返回插入后二叉搜索树的根节点. 保证原始二叉搜索树中不存在新值. 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可. 你可以返回任意有效的结果. 例如, 给定二叉搜索树: 4 / 2 7 / 1 3 和 插入的值: 5 你可以返回这个