-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0230-KthSmallestElementInABST.cs
43 lines (39 loc) · 1.21 KB
/
0230-KthSmallestElementInABST.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//-----------------------------------------------------------------------------
// Runtime: 96ms
// Memory Usage: 28.1 MB
// Link: https://leetcode.com/submissions/detail/342499617/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class _0230_KthSmallestElementInABST
{
public int KthSmallest(TreeNode root, int k)
{
var list = new List<int>();
InOrder(root, k, list);
return list[k - 1];
}
public void InOrder(TreeNode node, int k, IList<int> values)
{
if (values.Count >= k) return;
if (node == null) return;
InOrder(node.left, k, values);
values.Add(node.val);
InOrder(node.right, k, values);
}
}
}