二叉树的几种遍历方法

树的遍历是指访问树的每个节点,且每个节点仅被访问一次。二叉树的遍历可按二叉树的构成以及访问结点的顺序分为4种方式,即先序遍历、中序遍历、后序遍历、层序遍历。前后中三种遍历的区别在于同一节点在不同时刻被访问,在其各自的遍历结果序列中位置不同。 

一、先序遍历
二叉树先序遍历非递归方法
leetcode-114
方法一:
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        if(root==NULL)
            return vector<int>();
        stack<TreeNode*> st;
        st.push(root);
        vector<int> vi;
        TreeNode *cur;
        while(!st.empty()){
            cur = st.top();
            st.pop();
            vi.push_back(cur->val);
            if(cur->right!=NULL)
                st.push(cur->right);
            if(cur->left!=NULL)
                st.push(cur->left);
        }
        return vi;
    }
};

方法二:
class Solution {
public:

    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> vi;
        stack<TreeNode *> rights;
        TreeNode *cur = root;
        while(cur){
            vi.push_back(cur->val);
            if(cur->right)
                rights.push(cur->right);
            cur = cur->left;
            if(!cur && !rights.empty()){
                cur = rights.top();
                rights.pop();
            }

        }
        return vi;
    }
};

二、中序遍历

二叉树中序非递归遍历方法:
leetcode-94
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> vi;
        stack<TreeNode *> st;
        TreeNode *cur = root;
        while(cur!=NULL || !st.empty()){
            while(cur){
                st.push(cur);
                cur = cur->left;
            }

            cur = st.top();
            st.pop();
            vi.push_back(cur->val);
            cur= cur->right;

        }
        return vi;
    }
};

三、后序遍历

二叉树后序非递归遍历方法:

法一:
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> vi;
        if(!root) return vi;
        stack<TreeNode *> s1;
        stack<TreeNode *> s2;
        TreeNode *cur = root;
        s1.push(cur);
       
        while(!s1.empty()){
            cur = s1.top();
            s1.pop();
            s2.push(cur);
            if(cur->left)
                s1.push(cur->left);
            if(cur->right)
                s1.push(cur->right);
        }   
       
        while(!s2.empty()){
            cur = s2.top();
            s2.pop();
            vi.push_back(cur->val);
        }
       
        return vi;
    }
};

法二:
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> vi;
        if(!root) return vi;
        stack<TreeNode *> st;

        TreeNode *cur = NULL,*his = root;
        st.push(his);

        while(!st.empty()){
            cur = st.top();
            if(cur->left && cur->left!=his && cur->right!=his){
                st.push(cur->left);
            }else if(cur->right && cur->right != his){
                st.push(cur->right);
            }else{
                vi.push_back(cur->val);
                his = cur;
                st.pop();
            }
        }   

        return vi;
    }
};

法三:

思路:

pre-order traversal is root-left-right, and post order is left-right-root. modify the code for pre-order to make it root-right-left, and thenreverse the output so that we can get left-right-root .

  1. Create an empty stack, Push root node to the stack.
  2. Do following while stack is not empty.

2.1. pop an item from the stack and print it.

2.2. push the left child of popped item to stack.

2.3. push the right child of popped item to stack.

  1. reverse the ouput.

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> vi;
        if(!root) return vi;
        stack<TreeNode *> st;
       
        TreeNode *cur = root;
        st.push(cur);
        while(!st.empty()){
            cur = st.top();
            st.pop();
            vi.push_back(cur->val);
            if(cur->left)
                st.push(cur->left);
            if(cur->right)
                st.push(cur->right);
        }
        reverse(vi.begin(),vi.end());
        return vi;
    }
};



四、二叉树层序遍历

leetcode-102

class Solution {
public:
    

    vector<vector<int>> levelOrder(TreeNode* root) {
        if(root==NULL)
            return vector<vector<int>>();
        
        queue<TreeNode *> qt;
        qt.push(root);
        vector<vector<int>> vvi;
        TreeNode *cur;
        int sz;
        while(!qt.empty()){
            sz = qt.size();
            vector<int> vi;    
            for(int i=0;i<sz;i++){
                cur = qt.front();
                qt.pop();
                vi.push_back(cur->val);
                if(cur->left)
                    qt.push(cur->left);
                if(cur->right)
                    qt.push(cur->right);
            }
            vvi.push_back(vi);
        }
        
        return vvi;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值