树的遍历与图的遍历总结

树的先序遍历(非递归)

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        if(NULL == root) return result;
        stack<TreeNode *> mystack;
        mystack.push(root);
        while(!mystack.empty()){
            TreeNode* t = mystack.top();
            mystack.pop();
            if(NULL != t->right)
                mystack.push(t->right);
            if(NULL != t->left)
                mystack.push(t->left);
            result.push_back(t->val);
        }
        
        return result;
    }
};

树的后续遍历(非递归)

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        stack<TreeNode *> my_stack;
        vector<int> result;
        if(NULL == root) return result;
        TreeNode *lastVisited = root;
        my_stack.push(root);
        TreeNode *t;
        while(!my_stack.empty()) {
             t = my_stack.top();
            //my_stack.pop();
            while(NULL != t->left && lastVisited != t->left && lastVisited != t->right) {
                my_stack.push(t->left);
                t = t->left;
            }// end: t to the leftmost
            if(NULL != t->right && lastVisited != t->right)
            {
                my_stack.push(t->right);
            }
            else {
                result.push_back(t->val);
                lastVisited = t;
                my_stack.pop();
            }
        }
        
        return result;
    }
};

图的深入优先遍历(DFS), 类似于树的先序遍历:

Boolean visited[VEXNUM];

void DFSTraverse(Graph G, int (* Visit)(int v)) {
     for(v = 0; v<G.vexnum; ++v) visited[v] = FALSE;
     for(v = 0; v<G.vexnum; ++v) 
         if(!visited[v]) DFS(G,v);
}

void DFS(Graph G, int v) {
    visited[v] = TRUE; Visit(v);
    for( w=FirstAdjVex(G,v); w>=0; w=NextAdjVex(G,v,w))
         if(!visited[w]) DFS(G,w);
}

图的广度优先遍历(BFS),类似于树的层次遍历:

void BFSTraverse(Graph G, Status (* Visit)(int v))
{
    for(v = 0; v < G.vexnum; v++) visited[v] = FALSE;
    for(v = 0; v < G.vexnum; v++) 
         if(!visited[v]) {
                visited[v] = TRUE; Visit(v);
                EnQueue(q,v);
                while(!QueueEmpty(q)) {
                      DeQueue(q,u);
                      for( w = FirstAdjVex(G,u); w>=0; w=NextAdjVex(G,u,w))
                       {
                            if(!Visited[w]) {
                                   Visited[w] = TRUE; Visit(w);
                                   EnQueue(Q,w);
                             }
                       }
                }
         }
}

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/u012308776/article/details/39529959
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞