From 30c93fe6cc158c183a1bd21637e1d2e600326d86 Mon Sep 17 00:00:00 2001 From: anshika581 <52886143+anshika581@users.noreply.github.com> Date: Thu, 1 Oct 2020 18:10:35 +0530 Subject: [PATCH] Create kth smallest element in binary tree --- kth smallest element in binary tree | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 kth smallest element in binary tree diff --git a/kth smallest element in binary tree b/kth smallest element in binary tree new file mode 100644 index 0000000..13dccb0 --- /dev/null +++ b/kth smallest element in binary tree @@ -0,0 +1,11 @@ +public ArrayList list=new ArrayList<>(); + public int kthSmallest(TreeNode root, int k) { + if(root==null) return 0; + int l=kthSmallest(root.left,k); + if(l!=0) + return l; + list.add(root.val); + if(list.size()==k) return list.get(list.size()-1); + return kthSmallest(root.right,k); + + }