zoukankan      html  css  js  c++  java
  • 二叉树的重要遍历问题

    PS:本文主要包括二叉树的前序,中序,后序,层序,垂序遍历,使用cpp语言。 

    前序遍历

    struct TreeNode {
            int val;
            TreeNode* left;
            TreeNode* right;
            TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };
    class Solution {
    public:
            vector<int> preorderTraversal(TreeNode* root) {
                   stack<TreeNode*> sta;
                   vector<int> res; // 返回的数组
                   if (root == nullptr) return res; // 判断根节点,空树,直接返回
                   sta.push(root);
                   while (!sta.empty()) {
                           TreeNode* tmp = sta.top();
                           sta.pop();
                           if (tmp->right != nullptr) sta.push(tmp->right);
                           if (tmp->left != nullptr) sta.push(tmp->left);
                           res.push_back(tmp->val);
                   }
                   return res;
            }
    };

    中序遍历

    #include <iostream>
    #include <vector>
    #include <stack>
    
    using namespace std;
    
    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) {
            stack <TreeNode*> sta;
            vector <int> res;
            if (root == nullptr) return res;  // 空树, 直接返回res
            TreeNode* cur = root;
            while (cur != nullptr || !sta.empty) {
                // 先遍历左孩子
                if (cur != nullptr ){
                    sta.push(cur);
                    cur = cur->left;
                }
                else {
                    // 再 是 根结点
                    TreeNode* temp = sta.top();
                    sta.pop();  // 访问完根节点后就可以出栈,左孩子和根节点都访问完
                    res.push_back(temp->val);
                    // 最后右孩子
                    cur = temp->right;
                }
            }
            return res;
        }
    };

    后序遍历

    #include <iostream>
    #include <vector>
    #include <stack>
    
    using namespace std;
    
    struct TreeNode {
        int val;
        TreeNode* left;
        TreeNode* right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };
    
    // 第一种写法
    class Solution2 {
    public:
        vector<int> postorderTraversal(TreeNode* root) {
            stack<TreeNode*> sta;
            vector<int> res ;
            if (root == nullptr) return res;
            TreeNode* cur = root;  
            sta.push(cur);
            while(!sta.empty()){
                TreeNode* temp = sta.top();
                res.push_back(temp->val);
                sta.pop();
                if (temp->left) sta.push(temp->left);
                if (temp->right) sta.push(temp->right);
    
    
            }
            reverse(res.begin(),res.end());
            return res;   
        }
    };
    
    
    // 第二种方法 (重点理解)
    vector<int> PostOrderLoop(TreeNode* root)
    {
        stack<TreeNode*> sta;
        vector<int> ans;
        TreeNode* cur = root;
        TreeNode* pre = nullptr;
        while (cur || !sta.empty())
        {
            while (cur)
            {
                sta.push(cur);
                cur = cur->left;
            }
            cur = sta.top();
            //若右节点已经访问过或者没有右节点  则输出该节点值
            if (cur->right == nullptr || pre == cur->right) {
                sta.pop();
                ans.push_back(cur->val);
                pre = cur;
                cur = nullptr;
            }
            else {
                cur = cur->right;
                pre = nullptr;
            }
        }
        return ans;
    }

    层序遍历

    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    
    //Definition for a binary tree node.
    struct TreeNode {
        int val;
        TreeNode* left;
        TreeNode* right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };
    
    //  BFS
    class Solution {
    public:
        vector<vector<int>> levelOrder(TreeNode* root) {
            vector<vector<int>> res;
            if (root == nullptr) return res;
            queue<TreeNode*> que;
            que.push(root);
            while (!que.empty()) {
                int size = que.size();
                vector<int> level;
                while (size--) {
                    TreeNode* temp = que.front();
                    que.pop();
                    level.push_back(temp->val);
                    if (temp->left != nullptr) que.push(temp->left);
                    if (temp->right != nullptr) que.push(temp->right);
                }
                res.push_back(level);
            }
            return res;
        }
    };

    垂序遍历

    #include <iostream>
    #include <vector>
    #include <map>
    #include <algorithm>
    using namespace std;
    // 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:
             map<int, vector<pair<int, int>>> tree;
             void dfs(TreeNode* root, int x, int y) {
                    if (root == nullptr)  return;
                    tree[x].push_back(make_pair(y, root->val));
                    dfs(root->left, x - 1, y + 1);   // 将节点进行给予坐标处理
                    dfs(root->right, x + 1, y + 1);
             }
             vector<vector<int>> verticalTraversal(TreeNode* root) {
                    vector<vector<int>> res;
                    if (root == nullptr) return res;
                    dfs(root, 0, 0);
                    for (auto& node : tree) {    // 由于要修改node的值,需要加引用
                            sort(node.second.begin(), node.second.end());
                            vector<int> tmp;
                            for (auto t : node.second) {
                                   tmp.push_back(t.second);   // 获取val
                            }
                            res.push_back(tmp);
                    }
                    return res;
             }
    };
    

      

  • 相关阅读:
    wait函数和waitpid的使用和总结
    linux中sleep函数的使用和总结
    alarm()函数的使用总结
    linux定时器的实现方法
    Socket的长连接和短连接
    记录各种材质的数据
    max导出模型插件
    鸡汤 -心灵 记录
    UGUI 加载图片
    u3d udp服务器
  • 原文地址:https://www.cnblogs.com/E-Dreamer-Blogs/p/14673476.html
Copyright © 2011-2022 走看看