二叉树遍历的N种实现

二叉树遍历的N种实现

二叉树节点定义

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

递归实现

前序遍历

vector<int> ret;
vector<int> preorderTraversal(TreeNode* root) {
    if(root == NULL) return ret;

    ret.push_back(root->val);
    preorderTraversal(root->left);
    preorderTraversal(root->right);

    return ret;
}

中序遍历

vector<int> ret;
vector<int> inorderTraversal(TreeNode* root) {
    if(root == NULL) return ret;

    inorderTraversal(root->left);
    ret.push_back(root->val);
    inorderTraversal(root->right);

    return ret;
}

后序遍历

vector<int> ret;
vector<int> postorderTraversal(TreeNode* root) {
    if(root == NULL) return ret;

    postorderTraversal(root->left);
    postorderTraversal(root->right);
    ret.push_back(root->val);

    return ret;
}

迭代实现

前序遍历
用栈实现–方法1

vector<int> preorderTraversal(TreeNode *root){
    vector<int> ret;
    const TreeNode *p = root;
    stack<const TreeNode*> nodes;

    if(p != NULL) nodes.push(p);
    while(!nodes.empty()){
        p = nodes.top();
        nodes.pop();
        ret.push_back(p->val);

        if(p->right != NULL) nodes.push(p->right);
        if(p->left != NULL) nodes.push(p->left);
    }
    return ret;
}

用栈实现–方法2

vector<int> preorderTraversal(TreeNode *root){
    vector<int> ret;
    const TreeNode *p = root;
    stack<const TreeNode*> nodes;

    while(p != NULL || !nodes.empty()){
        if(p != NULL){
            ret.push_back(p->val);
            nodes.push(p);
            p = p->left;
        }else{
            p = nodes.top();
            nodes.pop();
            p = p->right; 
        }
    }
    return ret;
}

中序遍历
用栈实现

vector<int> inorderTraversal(TreeNode* root) {
     vector<int> ret;
     stack<const TreeNode*> nodes;
     const TreeNode *p = root;
     while(p != NULL || !nodes.empty()){
         if(p != NULL){
             nodes.push(p);
             p = p->left;
         }else{
             p = nodes.top();
             nodes.pop();
             ret.push_back(p->val);
             p = p->right;
         }
     }
     return ret;
 }

后序遍历
用栈实现–方法1

vector<int> postorderTraversal(TreeNode* root) {
    vector<int> ret;
    stack nodes;
    TreeNode *p, *q;
    p = root;
    do{
        while(p != NULL){
            nodes.push(p);
            p = p->left;
        }
        q = NULL;
        while(!nodes.empty()){
            p = nodes.top();
            if(p->right == q){
                nodes.pop();
                ret.push_back(p->val);
                q = p;
            }else{
                p = p->right;
                break;
            }
        }
    }while(!nodes.empty());

    return ret;
}

用栈实现–方法2

vector<int> postorderTraversal(TreeNode* root) {
    vector<int> ret;
    if(root == NULL) return ret;

    stack nodes;
    nodes.push(root);
    TreeNode *pre, *cur;
    while(!nodes.empty()){
        cur = nodes.top();
        if((cur->left == NULL && cur->right == NULL) || cur->left == pre || cur->right == pre){
            nodes.pop();
            ret.push_back(cur->val);
            pre = cur;
        }else{
            if(cur->right) nodes.push(cur->right);
            if(cur->left) nodes.push(cur->left);
        }
    }

    return ret;
}

用栈实现–方法3

vector<int> postorderTraversal(TreeNode* root) {
    vector<int> ret;
    stack<const TreeNode*> nodes;
    stack<const TreeNode*> current;
    const TreeNode *p = root;

    while(p != NULL || !current.empty()){
        if(p != NULL){
            nodes.push(p);
            if(p->left != NULL || p->right != NULL)
                current.push(p);
            p = p->right;
        }else{
            p = current.top();
            current.pop();
            p = p->left;
        }
    }

    while(!nodes.empty()){
        p = nodes.top();
        nodes.pop();
        ret.push_back(p->val);
    }
    return ret;
}

你可能感兴趣的:(算法,二叉树,遍历)