230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest
to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int kthSmallest(TreeNode root, int k) {
List<Integer> list=new ArrayList<>();
list=midTree(root); return list.get(k-1);
}
public List<Integer> midTree(TreeNode root){
List<Integer> list=new ArrayList<>(); if(root.left!=null)
list.addAll(midTree(root.left)); list.add(root.val);
if(root.right!=null)
list.addAll(midTree(root.right)); return list;
}
}
230. Kth Smallest Element in a BST的更多相关文章
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- 【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 ...
- 【刷题-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 ...
- [LeetCode] 230. 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 ...
- 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 ...
- 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 ...
- (medium)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 ...
- [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 ...
- LeetCode OJ 230. Kth Smallest Element in a BST
Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...
随机推荐
- [Js]JavaScript闭包和范围的快速测试
1. if (!("a" in window)) { var a = 1; } alert(a); [分析]代码含义:如果window不包含属性a,就声明一个变量a并赋值为1 ①J ...
- POJ 2762 tarjan缩点+并查集+度数
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15494 ...
- ECMAScript 6新特性(1)数组篇
数组现有的方法: .concat():连接两个或更多的数组,并返回结果. .join():把数组的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. .pop():删除并返回数组的最后一个元素 . ...
- HDU 3377 插头dp
题目大意: 从左上角走到右下角,每个点之多经过一次,取到所有路径上经过点的权值,求最大的权值之和,这里走到右下角就算停止了 这里有个思路是转化成熟悉的回路问题 在上方和右方最外围定义一圈权值为0 , ...
- HDU 1465
排列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 大家常常 ...
- Cisco IOS debug command reference Command A through D
debug aaa accounting through debug auto-config debug aaa accounting : to display information on acco ...
- android Xutils dbutils 注解
xUtils DbUtils 关于实体类注解 汇总 RockyZhang 发布于 1年前,共有 0 条评论 先来官方demo DbUtils db = DbUtils.create(this); ...
- linux卸载挂载点显示device is busy
在做umount设备时, device is busy是令人头痛的提示: [root@delphi /]# umount /dev/cdrom umount: /mnt/cdrom: device i ...
- SVN 首次用TortoiseSVN Checkout 提示Unexpected HTTP status 405
权限错误 首次使用 因为没有 弹出 用户名密码输入框 无法输入 帐户信息. 解决办法: 点击 仓库地址输入栏 右边 的...按钮 此时弹出的输入框浏览器方式访问的.输入用户名和密码,然后在左侧出来的仓 ...
- poj1181 大数分解
//Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...