-
Notifications
You must be signed in to change notification settings - Fork 1
/
KthSmallestElementBST.java
65 lines (53 loc) · 1.67 KB
/
KthSmallestElementBST.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.leetcode;
import java.util.LinkedList;
import java.util.List;
//https://leetcode.com/problems/kth-smallest-element-in-a-bst/
public class KthSmallestElementBST {
// No optimized , inorder till k nodes are added
class Solution {
public int kthSmallest(TreeNode root, int k) {
List<TreeNode> arr = new LinkedList<>();
inorder(root,arr,k);
return arr.get(k-1).val;
}
public void inorder(TreeNode node , List<TreeNode> arr , int k){
if(arr.size() == k) return;
if(node == null) return;
int val = node.val;
inorder(node.left,arr,k);
arr.add(node);
inorder(node.right,arr,k);
}
}
//We dont need to store all values . Memory optimized.
class Solution2 {
int count;
int result;
public int kthSmallest(TreeNode root, int k) {
count = k;
dfs(root);
return result;
}
public void dfs(TreeNode node){
if(node.left != null) dfs(node.left);
count--;
if(count == 0){//it will only happen once that count = 0 can also add if(count == 0) return; as first condition
result = node.val;
return;
}
if(node.right != null) dfs(node.right);
}
}
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;
}
}
}