Java solution using In-order traversal


  • 0
    K
        public int kthSmallest(TreeNode root, int k) {
            
            if(root == null){
                return 0;
            }
            
            List<Integer> l = new ArrayList<Integer>();
            kthSmallestUtil(root,l);
            return l.get(k-1);
            
        }
        
      
        public void kthSmallestUtil(TreeNode root, List<Integer> l){
            
            if(root == null){
                return;
            }
            kthSmallestUtil(root.left,l);
            l.add(root.val);
            kthSmallestUtil(root.right,l);
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.