Sunday, March 1, 2015

Remove one Recursion in Inorder Traversal

function inorder(Node root)
{
 if(root == NULL)
  return;
 inorder(root->left);
 print root->node;
 inorder(root->right);
}

After removing one recursion

function inorder(Node root)
{
 if(root == NULL)
  return;
 while(root->right != NULL) {
  inorder(root->left);
  print root->node;
  root = root->right;
 }
}

No comments:

Post a Comment