Skip to content

Commit

Permalink
Day 105
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagannath8 authored Apr 15, 2024
1 parent e77830b commit 71e4e8f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Kth Smallest Element In Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for binary tree
* class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) {
* val = x;
* left=null;
* right=null;
* }
* }
*/
public class Solution {
public int kthsmallest(TreeNode A, int B) {
Queue<TreeNode> q = new ArrayDeque<>();
ArrayList<Integer> ans = new ArrayList<>();
q.add(A);
while(!q.isEmpty()){
TreeNode curr = q.poll();
ans.add(curr.val);
if(curr.left != null)
q.add(curr.left);
if(curr.right != null)
q.add(curr.right);
}
Collections.sort(ans);
return ans.get(B-1);
}
}

0 comments on commit 71e4e8f

Please sign in to comment.