/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int KthSmallest(TreeNode root, int k) {
return KthSmallest_helper(root, ref k).val;
}
public TreeNode KthSmallest_helper(TreeNode root, ref int k) {
if(root == null) return null;
TreeNode num = KthSmallest_helper(root.left, ref k);
if(num != null) return num;
k--;
if(k == 0)
{
return root;
}
num = KthSmallest_helper(root.right, ref k);
if(num != null) return num;
return null;
}
}```