美文网首页
二叉树遍历

二叉树遍历

作者: ad丶leo | 来源:发表于2019-01-31 09:52 被阅读0次

    二叉树

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

    前序

    class Solution {
    public:
        vector<int> res;
        vector<int> preorderTraversal(TreeNode* root) {
            pre(root);
            return res;
        }
        void pre(TreeNode * root)
        {
            if(!root)
                return;
            res.push_back(root->val);
            if(root->left)
                pre(root->left);
            if(root->right)
                pre(root->right);
            return;
        }
    };
    
    class Solution {
    public:
        vector<int> preorderTraversal(TreeNode* root) {
            stack<TreeNode*> s;
            vector<int> res;
            if(root)
                s.push(root);
            while(!s.empty())
            {
                TreeNode* t=s.top();
                res.push_back(t->val);
                s.pop();
                if(t->right)
                    s.push(t->right);
                if(t->left)
                    s.push(t->left);
            }
            return res;
            
        }
    };
    

    中序

    class Solution {
    public:
        vector<int> res;
        vector<int> inorderTraversal(TreeNode* root) {
            in(root);
            return res;
            
        }
        void in(TreeNode* root)
        {
            if(!root)
                return;
            if(root->left)
                in(root->left);
            res.push_back(root->val);
            if(root->right)
                in(root->right);
            return;
        }
    };
    
    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode* root) {
            stack<TreeNode*>s;
            vector<int> res;
            TreeNode*p=root;
            while(p!=nullptr || !s.empty())
            {
                if(p!=nullptr)
                {
                    s.push(p);
                    p=p->left;
                }
                else
                {
                    p=s.top();
                    s.pop();
                    res.push_back(p->val);
                    p=p->right;
                }
            }
            return res;
            
        }
    };
    

    后序

    class Solution {
    public:
        vector<int> res;
        vector<int> postorderTraversal(TreeNode* root) {
            post(root);
            return res;
        }
        void post(TreeNode * root)
        {
            if(!root)
                return;
            if(root->left)
                post(root->left);
            if(root->right)
                post(root->right);
            res.push_back(root->val);
            return;
        }
    };
    
    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode* root) {
            stack<TreeNode*>s;
            vector<int>res;
            TreeNode * pre=nullptr;
            if (root!=nullptr)
                s.push(root);
            while(!s.empty())
            {
                TreeNode*p=s.top();
                if( (p->left==nullptr && p->right==nullptr) || 
                   (pre!=nullptr && (p->left==pre || p->right==pre)) )
                {
                    res.push_back(p->val);
                    s.pop();
                    pre=p;
                }
                else
                {
                    if(p->right!=nullptr)
                        s.push(p->right);
                    if(p->left!=nullptr)
                        s.push(p->left);
                }
                    
            }
            return res;
            
        }
    };
    

    层序

    class Solution {
    public:
        vector<vector<int>> res;
        vector<vector<int>> levelOrder(TreeNode* root) {
            level(root,1);
            return res;
        }
        void level(TreeNode * root,int l)
        {
            if(!root)   return;
            if(l>res.size())   res.push_back(vector<int>());
            res[l-1].push_back(root->val);
            level(root->left,l+1);
            level(root->right,l+1);
        }
    };
    
    public:
        vector<vector<int>> levelOrder(TreeNode* root) {
            queue<TreeNode*>q;
            if(root)
                q.push(root);
            vector<vector<int>> res;
            while(!q.empty())
            {
                vector<int> resi;
                int l=q.size();
                for(int i=0;i<l;i++)
                {
                    TreeNode * p=q.front();
                    resi.push_back(p->val);
                    q.pop();
                    if(p->left)
                        q.push(p->left);
                    if(p->right)
                        q.push(p->right);
                }
                res.push_back(resi);
            }
            return res;
            
        }
    };
    

    相关文章

      网友评论

          本文标题:二叉树遍历

          本文链接:https://www.haomeiwen.com/subject/ekwcsqtx.html