pk
BAN USERFelipe: Inorder traversal doesn't mean the output will be sorted from Binary tree. It simply means print LeftNodeRight. InOrder can be used on a BinaryTree or n-tree after a little modification.
However, if its a Binary Tree is a BST; Inorder will give sorted values otherwise not.
Why not to take first and last. Compare them and swap if needed till you reach middle
int[] arr = new int[10] { 0, 0, 1, 0, 1, 0, 1, 0, 0, 1};
int first = 0, last = arr.Length - 1,temp;
while(first <= last)
{
if(arr[first] == 1)
{
if (arr[last] == 0)
{
temp=arr[first];
arr[first] = arr[last];
arr[last] = temp;
first++;
}
last--;
}
else
{
first++;
}
}
Good out of the box solution.
- pk May 12, 2015this will get me though the this round :)