“`” 参考回答:

//深度优先搜索

//利用栈,现将右子树压栈再将左子树压栈

<pre><code>void DepthFirstSearch(BitNode *root)
{
stack<BitNode*> nodeStack;
nodeStack.push(root);
while (!nodeStack.empty())
{
BitNode *node = nodeStack.top();
cout << node->data << ' ';
nodeStack.pop();
if (node->right)
{
nodeStack.push(node->right);
}
if (node->left)
{
nodeStack.push(node->left);
}
}
}

</code></pre>

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.