1、 前序遍历
a) 概念:前序遍历首先访问根节点,然后遍历左子树,最后遍历右子树。
b) 实现方法:
i. 递归法(简洁)
/**
* 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) {
if(root!=NULL)
{
res.push_back(root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
return res;
}
private :vector<int> res;
};
ii. 迭代法(栈)
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: Preorder in vector which contains node values.
*/
vector<int> preorderTraversal(TreeNode *root) {
// write your code here
vector<int> result;
stack<TreeNode*> s;
if(root==NULL)
return result;
while(1){
if(root!=NULL)
result.push_back(root->val);
if(root->right!=NULL)
s.push(root);
if(root->left!=NULL)
root=root->left;
else if(!s.empty())
{
root=s.top()->right;
s.pop();
}
else
break;
}
}
};
2、 中序遍历
a) 概念:中序遍历是先遍历左子树,然后访问根节点,然后遍历右子树。
b) 实现方法:
i. 递归法(简洁)
/**
* 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) {
if(root!=NULL)
{
inorderTraversal(root->left);
res.push_back(root->val);
inorderTraversal(root->right);
}
return res;
}
private:vector<int> res;
};
ii. 迭代法(栈)
//iterate,using a stack
class Solution2 {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ret;
if(root==NULL)return ret;
TreeNode *curr=root;
stack<TreeNode*> st;
while(!st.empty()||curr!=NULL)
{
while(curr!=NULL)
{
st.push(curr);
curr=curr->left;
}
curr=st.top();
st.pop();
ret.push_back(curr->val);
curr=curr->right;
}
return ret;
}
};
3、 后序遍历
a) 概念:中序遍历是先遍历左子树,然后访问根节点,然后遍历右子树。
b) 实现方法:
i. 递归法
/**
* 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) {
if(root!=NULL)
{
postorderTraversal(root->left);
postorderTraversal(root->right);
res.push_back(root->val);
}
return res;
}
private:vector<int> res;
};
ii. 迭代法
/**
* 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) {
stack<TreeNode*> s;
vector<int> res;
TreeNode* last = NULL;
while(root || !s.empty()) {
if (root) {
s.push(root);
root = root->left;
}
else {
TreeNode* top = s.top();
if (top->right && top->right != last)
root = top->right;
else {
last = top;
res.push_back(top->val);
s.pop();
}
}
}
return res;
}
};
4、 层序遍历
a) 概念:层序遍历就是逐层遍历树结构。当我们在树中进行广度优先搜索时,我们访问的节点的顺序是按照层序遍历顺序的。
b) 广度优先搜索:是一种广泛运用在树或图这类数据结构中,遍历或搜索的算法。该算法从一个根节点开始,首先访问节点本身。 然后遍历它的相邻节点,其次遍历它的二级邻节点、三级邻节点,以此类推。
c) 实现方法:
i. 递归法
/**
* 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> > res;
if(root==NULL)
return res;
queue<TreeNode *> que;// 用队列 来记录遍历到的节点
que.push(root);
while(!que.empty())
{
int counts=que.size();
vector<int> temp;
while(counts--)
{
temp.push_back(que.front()->val);
if(que.front()->left)
que.push(que.front()->left);
if(que.front()->right)
que.push(que.front()->right);
que.pop();// 把已经用过的节点 弹走
}
res.push_back(temp);
}
return res;
}
};