Given an array of integers. Rotate the array by k position to right (or left). For example, given array A=[1,2,3,4,5,6] then rotateRight(A, 2) will return [5, 6, 1, 2, 3, 4] and rotateLeft(A,2) will return [3, 4, 5, 6, 1, 2]. Also, otateRight(A, 8) will return [5, 6, 1, 2, 3, 4] because after k=length […]
Tag Archives: constant space
Usual approach using linear space (stack) We can easily do the traversal by using a stack where we keep pushing left. If no left is there then we pop an element to print it and then push its right. This is O(n) time and O(n) space algorithm. public static void InorderTraversal(BTNode root){ Stack<BTNode> stack […]