Tree - 1005

版权声明:本文为博主原创文章,可以转载,仅用于分享 https://blog.csdn.net/hz2217/article/details/78993217

Tree - 1005

找 BUG
template <typename T>
void levelTraversal(BinaryNode<T>* root, void (*visit)(T &x))
{
    if(root) {
        stack<BinaryNode<T>*> nodeQueue; 
        stack<BinaryNode<T>*> a;  
        nodeQueue.push(root);  
        int k = 0; 
        while(k < nodeQueue.size())  
        {    
            BinaryNode<T>* temp;
        for(int j = nodeQueue.size() - 1 - k; j > 0; --j) {
            BinaryNode<T>* nodePop = nodeQueue.top();
            nodeQueue.pop();
            a.push(nodePop);
            }
        temp = nodeQueue.top();
        for(int j = 0; j < a.size(); ++j) {
            BinaryNode<T>* nodePop = a.top();
            a.pop();
            nodeQueue.push(nodePop);
            }   

            ++k;
            visit(temp->elem);

            if(temp->left)  
                nodeQueue.push(temp->left);  
            if(temp->right)  
                nodeQueue.push(temp->right);  
        }  
    }
}

猜你喜欢

转载自blog.csdn.net/hz2217/article/details/78993217
今日推荐