个人技术分享

DAY14(周二)

二叉树的递归遍历

144二叉树的前序遍历

过了。

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     void preorderfunction(TreeNode* cur,vector<int>&res)
  15.     {
  16.         if(cur==nullptrreturn;
  17.         res.push_back(cur->val);
  18.         preorderfunction(cur->left,res);
  19.         preorderfunction(cur->right,res);
  20.     }
  21.     vector<intpreorderTraversal(TreeNode* root) {
  22.         vector<int> res;
  23.         preorderfunction(root,res);
  24.         return res;
  25.     }
  26. };

145二叉树的后序遍历

过了

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     void postorderfunction(TreeNode* cur,vector<int>&res)
  15.     {
  16.         if(cur==nullptrreturn;
  17.         postorderfunction(cur->left,res);
  18.         postorderfunction(cur->right,res);
  19.         res.push_back(cur->val);
  20.     }
  21.     vector<intpostorderTraversal(TreeNode* root) {
  22.         vector<int> res;
  23.         postorderfunction(root,res);
  24.         return res;
  25.     }
  26. };

94二叉树的中序遍历

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     void inorderfunction(TreeNode* cur,vector<int> &res)
  15.     {
  16.         if(cur==nullptrreturn;
  17.         inorderfunction(cur->left,res);
  18.         res.push_back(cur->val);
  19.         inorderfunction(cur->right,res);
  20.     }
  21.     vector<intinorderTraversal(TreeNode* root) {
  22.         vector<int> res;
  23.         inorderfunction(root,res);
  24.         return res;
  25.     }
  26. };

迭代遍历

这里都需要二刷,来检验和加深印象。

前序遍历

图片来自代码随想录

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     vector<intpreorderTraversal(TreeNode* root) {
  15.         vector<int> res;
  16.         stack<TreeNode*> stk;
  17.         if(root==nullptrreturn res;
  18.         stk.push(root);
  19.         while(!stk.empty())
  20.         {
  21.             //先获取,以便进行迭代
  22.             TreeNode* cur=stk.top();
  23.             stk.pop();
  24.             res.push_back(cur->val);
  25.             //空结点不入栈,记住这里是要入栈,而不是单纯的更新cur
  26.             if(cur->right) stk.push(cur->right);
  27.             if(cur->left)  stk.push(cur->left);
  28.         }
  29.         return res;
  30.     }
  31. };

中序遍历

  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     vector<intinorderTraversal(TreeNode* root) {
  15.         vector<int> res;
  16.         stack<TreeNode*> stk;
  17.         TreeNode* cur=root;
  18.         if(root==nullptrreturn res;
  19.         while(cur!=NULL||!stk.empty())
  20.         {
  21.             if(cur!=NULL)//根据先进后出的栈特点 以及,中序遍历的特点:访问顺序与输出顺序相反,来入栈
  22.             {
  23.                 //访问过的都入栈
  24.                 stk.push(cur);
  25.                 cur=cur->left;//找最左的孩子
  26.             }else{
  27.                 cur=stk.top();//要处理它
  28.                 stk.pop();
  29.                 res.push_back(cur->val);//中。(自己是最左的,那么自己就没有左孩子了,自己就是中了)。
  30.                 //那么最左孩子的头上:中节点怎么办呢?不用着急,下一次循环会处理,因为有!stk.empty();
  31.                 cur=cur->right; //右
  32.             }
  33.         }
  34.         return res;
  35.     }
  36. };
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     vector<intpostorderTraversal(TreeNode* root) {
  15.         vector<int> res;
  16.         stack<TreeNode*> stk;
  17.         if(root==nullptrreturn res;
  18.         stk.push(root);
  19.         while(!stk.empty())
  20.         {
  21.             //先获取,以便进行迭代
  22.             TreeNode* cur=stk.top();
  23.             stk.pop();
  24.             res.push_back(cur->val);
  25.             //空结点不入栈,记住这里是要入栈,而不是单纯的更新cur
  26.             if(cur->left)  stk.push(cur->left);
  27.             if(cur->right) stk.push(cur->right);
  28.         }
  29.         reverse(res.begin(),res.end());
  30.         return res;
  31.     }
  32. };

后序遍历