DAY14(周二)
二叉树的递归遍历
144二叉树的前序遍历
过了。
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- void preorderfunction(TreeNode* cur,vector<int>&res)
- {
- if(cur==nullptr) return;
- res.push_back(cur->val);
- preorderfunction(cur->left,res);
- preorderfunction(cur->right,res);
- }
- vector<int> preorderTraversal(TreeNode* root) {
- vector<int> res;
- preorderfunction(root,res);
- return res;
- }
- };
145二叉树的后序遍历
过了
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- void postorderfunction(TreeNode* cur,vector<int>&res)
- {
- if(cur==nullptr) return;
- postorderfunction(cur->left,res);
- postorderfunction(cur->right,res);
- res.push_back(cur->val);
- }
- vector<int> postorderTraversal(TreeNode* root) {
- vector<int> res;
- postorderfunction(root,res);
- return res;
- }
- };
94二叉树的中序遍历
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- void inorderfunction(TreeNode* cur,vector<int> &res)
- {
- if(cur==nullptr) return;
- inorderfunction(cur->left,res);
- res.push_back(cur->val);
- inorderfunction(cur->right,res);
- }
- vector<int> inorderTraversal(TreeNode* root) {
- vector<int> res;
- inorderfunction(root,res);
- return res;
- }
- };
迭代遍历
这里都需要二刷,来检验和加深印象。
前序遍历
图片来自代码随想录
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<int> preorderTraversal(TreeNode* root) {
- vector<int> res;
- stack<TreeNode*> stk;
- if(root==nullptr) return res;
- stk.push(root);
- while(!stk.empty())
- {
- //先获取,以便进行迭代
- TreeNode* cur=stk.top();
- stk.pop();
- res.push_back(cur->val);
- //空结点不入栈,记住这里是要入栈,而不是单纯的更新cur
- if(cur->right) stk.push(cur->right);
- if(cur->left) stk.push(cur->left);
- }
- return res;
- }
- };
中序遍历
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<int> inorderTraversal(TreeNode* root) {
- vector<int> res;
- stack<TreeNode*> stk;
- TreeNode* cur=root;
- if(root==nullptr) return res;
- while(cur!=NULL||!stk.empty())
- {
- if(cur!=NULL)//根据先进后出的栈特点 以及,中序遍历的特点:访问顺序与输出顺序相反,来入栈
- {
- //访问过的都入栈
- stk.push(cur);
- cur=cur->left;//找最左的孩子
- }else{
- cur=stk.top();//要处理它
- stk.pop();
- res.push_back(cur->val);//中。(自己是最左的,那么自己就没有左孩子了,自己就是中了)。
- //那么最左孩子的头上:中节点怎么办呢?不用着急,下一次循环会处理,因为有!stk.empty();
- cur=cur->right; //右
- }
- }
- return res;
- }
- };
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<int> postorderTraversal(TreeNode* root) {
- vector<int> res;
- stack<TreeNode*> stk;
- if(root==nullptr) return res;
- stk.push(root);
- while(!stk.empty())
- {
- //先获取,以便进行迭代
- TreeNode* cur=stk.top();
- stk.pop();
- res.push_back(cur->val);
- //空结点不入栈,记住这里是要入栈,而不是单纯的更新cur
- if(cur->left) stk.push(cur->left);
- if(cur->right) stk.push(cur->right);
- }
- reverse(res.begin(),res.end());
- return res;
- }
- };