二叉树的基本遍历算法

基本数据结构

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

前序遍历

class Solution {
public:
 vector preorderTraversal(TreeNode *root)
    {
        vector visit;
        prevIterativeTraversal(root, visit);
        return visit;
    }
    
    void prevTraversal(TreeNode * root, vector &visit)
    {
        if(root == NULL)
            return;
        else
        {
            visit.push_back(root->val); 
            prevTraversal(root->left, visit);
            prevTraversal(root->right,visit);
        }
    }
    void prevIterativeTraversal(TreeNode *root, vector &visit)
    {
        stack nodeStack;
        TreeNode *iterator = root;
        TreeNode *prev = root;
        iterator = root;
        while(iterator != NULL || !nodeStack.empty())
        {
            while(iterator != NULL)            //一直搜索该结点的左孩子
            {
                prev = iterator;
                nodeStack.push(iterator);
                visit.push_back(iterator->val);
                iterator = iterator->left;
            }
            iterator = nodeStack.top();  //弹出结点
            nodeStack.pop(); 
            iterator = iterator->right;   //访问该结点的右孩子
        }
    }
};
中序遍历

class Solution {
public:
    vector midorderTraversal(TreeNode *root)
    {
        vector visit;
        midIterativeTraversal(root, visit);
        return visit;
    }

    void midTraversal(TreeNode * root, vector &visit)
    {
        if(root == NULL)
            return;
        else
        {
            midTraversal(root->right,visit);
            visit.push_back(root->val); 
            midTraversal(root->left, visit);
        }
    }
    void midIterativeTraversal(TreeNode *root, vector &visit)
    {
        stack nodeStack;
        TreeNode *iterator = root;
        TreeNode *prev = root;
        iterator = root;
        while(iterator != NULL || !nodeStack.empty())  
        {
            while(iterator != NULL)     
            {
                prev = iterator;
                nodeStack.push(iterator);
                iterator = iterator->left;
            }
            iterator = nodeStack.top();
            visit.push_back(iterator->val);    //当该节点的右子树访问完毕的时候访问该节点
            nodeStack.pop();
            iterator = iterator->right;
        }
    }
};
后序遍历

class Solution {
public:
    vector postorderTraversal(TreeNode *root)
    {
        vector visit;
        postIterativeTraversal(root, visit);
        return visit;
    }
    void postTraversal(TreeNode * root, vector &visit)
    {
        if(root == NULL)
            return;
        else
        {
            postTraversal(root->left, visit);
            postTraversal(root->right,visit);
            visit.push_back(root->val); 
        }
    }
    void postIterativeTraversal(TreeNode *root, vector &visit)
    {
        stack nodeStack;
        TreeNode *iterator = root;
        TreeNode *prev = root;
        iterator = root;
        while(iterator != NULL || !nodeStack.empty())
        {
            while(iterator != NULL)           //如果该结点不为空,那么一直寻找左孩子结点
            {
                prev = iterator;
                nodeStack.push(iterator);
                iterator = iterator->left;
            }
            prev = iterator;
            iterator = nodeStack.top();     
            while (iterator->right == prev)   //判断当前结点的右子树是否已经遍历完毕
            {
                nodeStack.pop();            
                visit.push_back(iterator->val);     //当右子树遍历完毕便可以写入该结点的值
                if(nodeStack.empty())
                    return;
                prev = iterator;
                iterator = nodeStack.top();
            }
            prev = iterator;
            iterator = iterator->right;

        }
    }
};
层次遍历

class Solution {
public:
    vector levelorderTraversal(TreeNode *root)
    {
        vector visit;
        queue nodeQueue;
        TreeNode *iterator = NULL;
        if(root != NULL)
            nodeQueue.push(root);
        while (!nodeQueue.empty())
        {
            iterator = nodeQueue.front();
            nodeQueue.pop();
            visit.push_back(iterator->val);
            if (iterator->left != NULL)
                nodeQueue.push(iterator->left);
            if (iterator->right != NULL)
                nodeQueue.push(iterator->right);

        }
        return visit;
    }
};

你可能感兴趣的:(基本算法,leetcode,题目)