zoukankan      html  css  js  c++  java
  • 树的遍历

    It's just a personal note.

    Recursion traversal is trivial. All the traversal methods are implemented by iteration. Here is the source code.

    Binary Tree Traversal

    The data structure of binary tree node is:

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

      vector<int> traversal;
      void preorder(TreeNode *root)
      {
          if (root == nullptr)
              return;
          auto p = root;
          stack<TreeNode *> s;
          s.push(p);
          while (!s.empty())
          {
              p = s.top(), s.pop();
              traversal.push_back(p->val);
              if (p->right != nullptr)   s.push(p->right);
              if (p->left != nullptr)    s.push(p->left);
          }
      }
      
    • Inorder

      The first push operation outside the loop is not required here.

      vector<int> traversal;
      void inorder(TreeNode *root)
      {
          if (root == nullptr)
              return;
          auto p = root;
          stack<TreeNode *> s;
          // do not push(p) here
          while (!s.empty() || p != nullptr)
          {
              if (p != nullptr)
              {
                  s.push(p);
                  p = p->left;
              }
              else
              {
                  p = s.top(), s.pop();
                  traversal.push_back(p->val);
                  p = p->right;
              }
          }
      }
      
    • Postorder

      Aside: Variable traversal can use stack instead of vector.

      vector<int> traversal;
      void postorder(TreeNode *root)
      {
          if (root == nullptr)
              return;
          auto p = root;
          stack<TreeNode *> s;
          s.push(p);
          while (!s.empty())
          {
              p = s.top(), s.pop();
              traversal.push_back(p->val);
              if (p->left != nullptr)
                  s.push(p->left);
              if (p->right != nullptr)
                  s.push(p->right);
          }
          reverse(traversal.begin(), traversal.end());
      }
      
    • Create

      // -1 means null node
      vector<int> data({1, 2, 3, -1, 4, -1, 5});
      TreeNode *root = create(data, 0);
      TreeNode *create(const vector<int> &v, int idx)
      {
          if (idx >= (int)v.size() || v[idx] == -1)  return nullptr;
          TreeNode *p = new TreeNode(v[idx]);
          p->left = create(v, 2 * idx + 1);
          p->right = create(v, 2 * idx + 2);
          return p;
      }
      

    N-ary Tree Traversal

    The data structure of N-ary tree node is:

    class Node
    {
    public:
        int val;
        vector<Node *> children;
        Node() {}
        Node(int _val) { val = _val; }
        Node(int _val, vector<Node *> _children)
        {
            val = _val;
            children = _children;
        }
    };
    
    • Preorder

      vector<int> result;
      void preorderRecursion(Node *p)
      {
          if (p == nullptr)  return;
          result.push_back(p->val);
          for (auto x : p->children)
              preorderRecursion(x);
      }
      void preorderIteration(Node *root)
      {
          auto p = root;
          stack<Node *> s;
          s.push(p);
          while (!s.empty())
          {
              p = s.top(), s.pop();
              result.push_back(p->val);
              for (auto i = p->children.rbegin(); i != p->children.rend(); i++)
              {
                  s.push(*i);
              }
          }
      }
      
    • Inorder

      Undefined behavior in N-ary tree.

    • Postorder

      vector<int> result;
      void postorderRecursion(Node *p)
      {
          if (p == nullptr)  return;
          for (auto x: p->children)
              postorderRecursion(x);
          result.push_back(p->val);
      }
      void postorderIteration(Node *root)
      {
          auto p = root;
          stack<Node *> s;
          s.push(p);
          while (!s.empty())
          {
              p = s.top(), s.pop();
              result.push_back(p->val);
              for (auto x : p->children)
                  s.push(x);
          }
          reverse(result.begin(), result.end());
      }
      
  • 相关阅读:
    shell预备知识
    php session和cookie
    php数据库访问抽象层PDO
    递归求斐波那契数
    SqlDataReader的使用
    DataAdapter与DataSet的使用
    CLR
    Android Studio javadoc 生成注释文档
    MySQL教程之concat以及group_concat的用法
    一文读懂MQTT协议
  • 原文地址:https://www.cnblogs.com/sinkinben/p/12662958.html
Copyright © 2011-2022 走看看