二叉搜索树的递归遍历和非递归遍历

  • 递归遍历
  void preOrder(TreeNode *root){
    if(!root) return;
    cout << root->val << endl;
    preOrder(root->left);
    preOrder(root->right);
}   
void inOrder(TreeNode *root){
    if(!root) return;
    inOrder(root->left);
    cout << root->val << endl;
    inOrder(root->right);
}  
void postOrder(TreeNode *root){
    if(!root) return;
    postOrder(root->left);
    postOrder(root->right);
    cout << root->val << endl;
}
  • 非递归遍历
//非递归先序遍历两种方法,一种是在进栈的时候打印,一种是在出栈的时候打印。
//在出栈的时候打印 第一种方式比较容易理解
void preOrder1(TreeNode *root){
    if(!root) return;
    stack stack;
    stack.push(root);
    while(!stack.empty()){
        TreeNode *tmp = stack.top();
        stack.pop();
        cout << tmp->val << endl;
        if(stack->right) stack.push(stack->right);
        if(stack->left) stack.push(stack->left);
    }
}

//在入栈的时候打印
void preOrder2(TreeNode *root){
    if(!root) return;
    stack stack;
    TreeNode *node = root;
    while(node || !stack.empty()){
        while(node != NULL){
            cout << node->val << endl;
            stack.push(node);
            node = node->left;
        }

        if(!stack.empty()){
            node = stack.top(); // 回溯到父节点
            stack.pop();
            node = node->right;  //访问右子树
        }
    }
}    

//中序遍历非递归实现版
void inOrder1(TreeNode *root){
    if(!root) return;
    stack stack;
    TreeNode *node = root;
    while(node || !stack.empty()){
        while(node){
            stack.push(node);
            node = node->left;
        }
        if(!stack.empty()){
            TreeNode *tmp = stack.top();
            stack.pop();
            cout << tmp->val << endl;
            node = node->right;
        }
    }
}   

//后续遍历实现的难点就是先要遍历完左右节点,然后去遍历根节点。
//第一种方法,利用双栈实现  利用双栈的方式,首先是将树按照根,右子树,左子树,压入输出栈,再从输出栈压出就是后续遍历

void postOrder1(TreeNode *root){
    if(!root) return;
    stack stack1, stack2;
    stack1.push(root);
    while(!stack1.empty()){
        TreeNode *node = stack1.top();
        stack1.pop();
        stack2.push(node);
        if(node->left) stack1.push(node->left);
        if(node->right) stack1.push(node->right);

    }

    while(!stack2.empty()){
        cout << stack2.top()->val << endl;
        stack2.pop();
    }

}

//第二种方法 只要左右节点都访问过就可以访问了 这种方式比较难想,还需要设置一个pre指针才行。
void postOrder2(TreeNode *root){
    stack stack;
    TreeNode *cur, *pre = NULL;
    stack.push(root);
    while(!stack.empty()){
        cur = stack.top();
        if(!cur->left && !cur->right || (pre && (pre == cur->left || pre == cur->ritht))){
            cout << cur->val << endl;
            stack.pop();
            pre = cur;
        }
        else{
            if(cur->right) stack.push(cur->right);
            if(cur->left) stack.push(cur->left);
        }
    }
}

层序遍历

   // 简单层序遍历
void levelorder(node *root){
    queue q;
    if(!root) return;
    q.push(root);
    while(!q.empty()){
        node *cur = q.front();
        q.pop();
        if(cur->left) q.push(cur->left);
        if(cur->right) q.push(cur->right);
    }
}    

// 记录每层的节点个数的层序遍历  
    vector<vector<int> > levelOrder(TreeNode *root) {
        vector<vector<int> > res;
        if (root == NULL) return res;

        queue q;
        q.push(root);
        while (!q.empty()) {
            vector<int> oneLevel;
            int size = q.size();
            for (int i = 0; i < size; ++i) {
                TreeNode *node = q.front();
                q.pop();
                oneLevel.push_back(node->val);
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
            res.push_back(oneLevel);
        }
        return res;
    }   

你可能感兴趣的:(algorithm,learning)