Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 564 Bytes

230.kth-smallest-element-in-a-bst.md

File metadata and controls

28 lines (23 loc) · 564 Bytes
  • 二叉搜索树的性质

  • 全局变量辅助求解

代码实现

Java

class Solution {
    int ans, k;
    public int kthSmallest(TreeNode root, int _k) {
        k = _k;
        dfs(root);
        return ans;
    }
    boolean dfs(TreeNode root){
        if (root == null) return false;
        if (dfs(root.left)) return true;
        if (--k == 0) {
            ans = root.val;
        }
        return dfs(root.right);
    }
}