1.前序遍历
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 {
vector<int> re;
public:
vector<int> preorderTraversal(TreeNode* root) {
if(root){
re.push_back(root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}return re;
}
};
2.迭代
(1)
1.根节点入栈,出栈。
2.右子树先入栈,然后左子树入栈。
3.栈顶元素出栈
/**
* 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> re;
stack<TreeNode*> s;
TreeNode* p=root;
if(p!=NULL) s.push(p);
while(!s.empty()){
p=s.top();
s.pop();
re.push_back(p->val);
if(p->right!=NULL){
s.push(p->right);
}
if(p->left!=NULL){
s.push(p->left);
}
}
return re;
}
};
(2)
1.结点P入栈。
2.P的左子树若不为空,则将P的左子树置为当前的结点,输出p,若为空,出栈并将栈顶结点的右子树置为当前的结点P,循环。
3.直到P为NULL并且栈为空,则遍历结束。
/**
* 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> re;
stack<TreeNode*> s;
TreeNode* p=root;
while(p!=NULL||!s.empty()) {
while(p!=NULL)
{
s.push(p);
re.push_back(p->val);
p=p->left;
}
if(!s.empty())
{
p=s.top();
s.pop();
p=p->right;
}
}return re;
}
};
2.中序遍历
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 {
vector<int> re;
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root){
inorderTraversal(root->left);
re.push_back( root->val);
inorderTraversal(root->right);
}
return re;
}
};
2.迭代
1.根节点入栈。
2.循环:栈顶元素左子树存在,则左子树入栈,若左子树不存在,输出栈顶元素,并检查右子树是否存在,若存在,右子树入栈。
3.栈空时结束。
/**
* 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> re;
stack<TreeNode*> s;
TreeNode* p=root;
while(p!=NULL||!s.empty()){
while(p!=NULL){
s.push(p);
p=p->left;
}
if(!s.empty()){
p=s.top();
s.pop();
re.push_back(p->val);
p=p->right;
}
}
return re;
}
};
3.后序遍历
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 {
vector<int> re;
public:
vector<int> postorderTraversal(TreeNode* root) {
if(root){
postorderTraversal(root->left);
postorderTraversal(root->right);
re.push_back( root->val);
}
return re;
}
};
2.迭代
(1)
逆后序序列是前序遍历左右子树交换的结果,即将逆后序序列逆序就可以得到后序序列。
ps:也可以使用两个栈实现。
/**
* 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> re;
stack<TreeNode*> s;
TreeNode* p=root;
if(p!=NULL) s.push(p);
while(!s.empty()){
p=s.top();
s.pop();
re.push_back(p->val);
if(p->left!=NULL){
s.push(p->left);
}
if(p->right!=NULL){
s.push(p->right);
}
}
reverse(re.begin(),re.end());
return re;
}
};
(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> re;
stack<TreeNode*> s;
TreeNode* p=root;
TreeNode* p2 = NULL; // 指向前一个被访问的节点
while(!s.empty()||p!=NULL){
while(p!=NULL) {
s.push(p);
p=p->left;
}
p=s.top();
if(p->right== NULL || p->right == p2) // 当前节点的右子树如果为空或者已经被访问,则访问当前节点
{
re.push_back(p->val);
p2=p;
s.pop();
p=NULL;
}
else{
p=p->right;
}
}
return re;
}
};
4.层次遍历
/**
* 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<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> re;
if(!root) return re;
queue<TreeNode*> q;
TreeNode* p=root;
q.push(p);
while(!q.empty()){
vector<int> s;
int n=q.size();
while(n--){
p=q.front();
s.push_back(p->val);
q.pop();
if(p->left) q.push(p->left);
if(p->right) q.push(p->right);
}
re.push_back(s);
}
return re;
}
};
来源:CSDN
作者:暴雨是樱桃味der-
链接:https://blog.csdn.net/weixin_41545506/article/details/103475182