#该文章代码参考慕课网——>玩转算法面试 从真题到思维全面提升算法思维#
二叉树的遍历方式从大的方面可以分为两种:深度优先遍历和广度优先遍历。
深度优先搜索(Depth First Search),是沿着树的深度遍历树的节点,尽可能深的搜索树的分支,属于纵向遍历。
广度优先搜索(Breadth First Search),又叫宽度优先搜索或横向优先搜索,是从根结点开始沿着树的宽度搜索遍历。
具体遍历顺序区别,如下图:
按照深度优先遍历有三种方式:分别为前序遍历、中序遍历、后序遍历。
前序遍历:根结点 ---> 左子树 ---> 右子树
结果为:ABDECFG
中序遍历:左子树---> 根结点 ---> 右子树
结果为:DBEAFCG
后序遍历:左子树 ---> 右子树 ---> 根结点
结果为:DEBFCGA
按照广度优先遍历有两种方式:从上至下的层序遍历、从下至上的层序遍历。
从上至下的层序遍历:从上至下,从左至右逐个节点遍历
结果为:ABCDEFG
从下至上的层序遍历:从下至上,从左至右逐个节点遍历
结果为:DEFGBCA
此处三个代码并非直接利用递归语句实现,而是采用了数据结构中的栈这种数据结构来模拟递归,实现二叉树的深度优先遍历。此处为该代码与众不同之处。
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
reeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct Command {
string command;
TreeNode* node;
Command(string s,TreeNode* n):command(s),node(n){}
};
class Solution {
public:
vector preorderTraversal(TreeNode* root) {
stack stack;
vector result;
if(root==NULL)
return result;
else{
stack.push(Command("go",root));
while(!stack.empty())
{
Command command=stack.top();
stack.pop();
if(command.command=="print")
result.push_back(command.node->val);
else
{
if(command.node->right)
stack.push(Command("go",command.node->right));
if(command.node->left)
stack.push(Command("go",command.node->left));
stack.push(Command("print",command.node));
}
}
return result;
}
}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct Command {
string command;
TreeNode* node;
Command(string s,TreeNode* n):command(s),node(n){}
};
class Solution {
public:
vector inorderTraversal(TreeNode* root) {
stack stack;
vector result;
if(root==NULL)
return result;
else{
stack.push(Command("go",root));
while(!stack.empty())
{
Command command=stack.top();
stack.pop();
if(command.command=="print")
result.push_back(command.node->val);
else
{
if(command.node->right)
stack.push(Command("go",command.node->right));
stack.push(Command("print",command.node));
if(command.node->left)
stack.push(Command("go",command.node->left));
}
}
return result;
}
}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct Command {
string command;
TreeNode* node;
Command(string s,TreeNode* n):command(s),node(n){}
};
class Solution {
public:
vector postorderTraversal(TreeNode* root) {
stack stack;
vector result;
if(root==NULL)
return result;
else{
stack.push(Command("go",root));
while(!stack.empty())
{
Command command=stack.top();
stack.pop();
if(command.command=="print")
result.push_back(command.node->val);
else
{
stack.push(Command("print",command.node));
if(command.node->right)
stack.push(Command("go",command.node->right));
if(command.node->left)
stack.push(Command("go",command.node->left));
}
}
return result;
}
}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector> levelOrder(TreeNode* root) {
vector> result;
queue > queue;
queue.push(make_pair(0,root));
if(root==NULL)
return result;
else{
while(!queue.empty()){
int level=queue.front().first;
TreeNode* node=queue.front().second;
queue.pop();
if(level==result.size())
result.push_back(vector());
result[level].push_back(node->val);
if(node->left)
queue.push(make_pair(level+1,node->left));
if(node->right)
queue.push(make_pair(level+1,node->right));
}
}
return result;
}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector> levelOrderBottom(TreeNode* root) {
deque> result1;
queue > queue;
queue.push(make_pair(0,root));
if(root==NULL)
return vector>();
else{
while(!queue.empty()){
int level=queue.front().first;
TreeNode* node=queue.front().second;
queue.pop();
if(level==result1.size())
result1.push_front(vector());
result1[0].push_back(node->val);
if(node->left)
queue.push(make_pair(level+1,node->left));
if(node->right)
queue.push(make_pair(level+1,node->right));
}
}
return vector>(result1.begin(),result1.end());
}
};