方法1、暴力递归
/**
* 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;
inorder(root, res);
return res;
}
void inorder(TreeNode *root, vector<int> &res) {
if (root != nullptr) {
inorder(root->left, res);
res.push_back(root->val);
inorder(root->right, res);
}
}
};
方法 2、颜色标记法
这里每个结点都遍历两次!
使用栈来深度遍历二叉树,只有非空节点才压入栈中 非空节点才压入栈中 非空节点才压入栈中
这里需要用到三种数据结构
vector
#include <vector>
- 添加元素
push_back()
stack
#include <stack>
- 添加元素(压栈)
push()
栈顶元素top()
删除元素(出栈)pop()
pair
对于pair一定要消除一个误区,pair跟map没有绝对关系,并不是在map下才有pair。由于是学map时学的pair给人一种错觉。
#include <utility>
- 创建pair的两种方法
//方法1 跟所有类创建对象一样,也就是使用构造函数 pair<TreeNode *, int> my_pair(root, 1); pair<TreeNode *, int>(root, 1); //匿名pair对象 //方法2 使用make_pair函数模板,其实就是利用拷贝构造函数! pair<TreeNode *, int> my_pair = make_pair(root, 1); make_pair(root, 1); //匿名pair对象
/**
* 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) {
std::vector<int> res;
if(root == NULL)
return res;
std::stack<pair<TreeNode *, int>> stk;
stk.push(pair<TreeNode *, int>(root, 1));
while(!stk.empty()){
pair<TreeNode *, int> tmp = stk.top();
stk.pop();
if(tmp.second == 0)
res.push_back(tmp.first->val);
else{
if(tmp.first->right != NULL) // 非空节点才压入栈中
stk.push(pair<TreeNode *, int>(tmp.first->right, 1));
stk.push(pair<TreeNode *,int>(tmp.first, 0));
if(tmp.first->left != NULL) //非空节点才压入栈中
stk.push(pair<TreeNode *, int>(tmp.first->left, 1));
}
}
return res;
}
};
方法3、左链入栈
/**
* 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;
auto p = root;
while (p) {
stk.push(p);
p = p->left;
}
while (!stk.empty()) {
auto tmp = stk.top();
stk.pop();
res.push_back(tmp->val);
p = tmp->right;
while (p) {
stk.push(p);
p = p->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;
if (root == nullptr) return res;
stack<TreeNode *> stk;
auto p = root;
while (p != nullptr || !stk.empty()) {
while (p != nullptr) {
stk.push(p);
p = p->left;
}
auto tmp = stk.top();
stk.pop();
res.push_back(tmp->val);
p = tmp->right;
}
return res;
}
};