代码题(8)— 二叉树的遍历:前、中、后序
1、144. 二叉树的前序遍历
(1)递归
/** * 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: vector<int> res; vector<int> preorderTraversal(TreeNode* root) { if(root == nullptr) return res; res.push_back(root->val); preorderTraversal(root->left); preorderTraversal(root->right); return res; } };
(2)非递归
/** * 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: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if(root == nullptr) return res; stack<TreeNode*> stNode; while(!stNode.empty() || root) { while(root) { stNode.push(root); res.push_back(root->val); root = root->left; } root = stNode.top(); stNode.pop(); root = root->right; } return res; } };
2、94. 二叉树的中序遍历
(1)递归
/**
* 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:
vector<int> res;
vector<int> inorderTraversal(TreeNode* root) {
if(root != nullptr)
{
inorderTraversal(root->left);
res.push_back(root->val);
inorderTraversal(root->right);
}
return res;
}
};
(2)非递归
/**
* 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:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if(root == nullptr)
return res;
stack<TreeNode*> stNode;
while(!stNode.empty() || root)
{
while(root)
{
stNode.push(root);
root = root->left;
}
root = stNode.top();
stNode.pop();
res.push_back(root->val);
root = root->right;
}
return res;
}
};
3、145. 二叉树的后序遍历
(1)递归
/** * 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: vector<int> res; vector<int> postorderTraversal(TreeNode* root) { if(root == nullptr) return res; postorderTraversal(root->left); postorderTraversal(root->right); res.push_back(root->val); return res; } };
(2)非递归
/** * 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: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; if(root == nullptr) return res; stack<TreeNode*> stNode; TreeNode* pre = nullptr; while(!stNode.empty() || root) { while(root) { stNode.push(root); root = root->left; } TreeNode* cur = stNode.top(); if(cur->right == nullptr || pre == cur->right) //如果没有右节点,或者右节点被访问过,则可以访问该节点 { res.push_back(cur->val); stNode.pop(); pre = cur; } else root = cur->right; } return res; } };
该方法与上面区别是:不需要添加新的节点。
/** * 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: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; if(root == nullptr) return res; stack<TreeNode*> st; TreeNode* pre = nullptr; while(!st.empty() || root) { while(root) { st.push(root); root = root->left; } root = st.top(); //此处没有添加新的节点 if(root->right == nullptr || root->right == pre) { res.push_back(root->val); st.pop(); pre = root; root = nullptr;//此处添加将root置为空,不再访问判断该节点。 } else root = root->right; } return res; } };