inorder traversal without using recursion

 

inorder traversal  without using recursion

vector<int> inOrder(Node* root)
{
  vector<int>v;
  stack<Node*>st;
 
 
 
  while(root!=NULL  ||  !st.empty())
  {
      while(root)
      {
          st.push(root);
          root=root->left;
          
      }
      root=st.top();
      st.pop();
      
      v.push_back(root->data);
      
      root=root->right;
      
  }
  return v;
 
}

Comments

Popular posts from this blog

Amazing Subarrays(cpp,interviewbit)

Symmetric Tree(leetcode,cpp):

sum of left leaves in a tree(leetcode).