Print out the k smallest element from a BST into a specified array, in the function form: firstKelement(treenode *root, int k, int *arr), without using any global variables.
Anonymous
private static int[] printSmallestK(TreeNode root, int k, int[] a) { Stack<div>s = new Stack(); while(true) { if(root != null) { s.push(root); root = root.left; } else { if(s.isEmpty()) break; root = s.pop(); if(k > 0) { a[k-1] = root.key; k--; } root = root.right; } } return a; }</div>
Check out your Company Bowl for anonymous work chats.