二叉树
二叉搜索树的中序遍历是递增序列。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int ret,k;
void dfs(TreeNode root){
if(root==null) return;
dfs(root.left);
if(k==0) return;
if(--k==0) ret=root.val;
dfs(root.right);
}
public int kthSmallest(TreeNode root, int k) {
this.k=k;
dfs(root);
return ret;
}
}