void BinaryTree::SpiralTraversal(Node* root)
{
stack<Node*> stack1, stack2;
stack1.push(root);
while(!stack1.empty() || !stack2.empty())
{
while(!stack1.empty())
{
Node* node1 = stack1.top();
stack1.pop();
cout<<node1->data<<'\n';
if(node1->right)
stack2.push(node1->right);
if(node1->left)
stack2.push(node1->left);
}
while(!stack2.empty())
{
Node* node2 = stack2.top();
stack2.pop();
cout<<node2->data<<'\n';
if(node2->left)
stack1.push(node2->left);
if(node2->right)
stack1.push(node2->right);
}
}
}
No comments:
Post a Comment